python 學習 D3

來源:互聯網
上載者:User

標籤:字串   學習   []   format   返回   教材   alpha   用法   部分   

  基礎類型初識 

2.1數字int。

數字主要是用於計算用的,使用方法並不是很多,就記住一種就可以:

2.2 字串 str  儲存少量的資料

    ‘alex‘ ,‘1235443543‘

    ‘[1,2,3]‘

3.3 布爾值 bool

    True  False 

4.4  列表 list      各種資料類型的資料,大量的資料,便於操作.

       [‘name‘,True,[]....],

 5.5 元祖 tupe   唯讀列表 

    用 ()  表示    (1,2,3)

 6.6  字典  dict      儲存大量的資料,關係型資料。 索引值對(key-value)的形式儲存資料

{‘name‘:‘老男孩‘,

            ‘name_list‘:[‘反面教材‘,.....]

            ‘alex‘:{‘age‘:40,

                    ‘hobby‘:‘old_women‘,

                    }

            },

  7.7  set  

        {‘wusir‘, ‘alex‘, ....}

       

具體 的用法  

 int   

#i = 100#print(i.bit_length()) 十進位轉化成二進位的有效位元  1          0000 0001  2          0000 0010  3          0000 0011 100  bool

 

                       int <---> str    互相轉化  str ---> int    int(str)  條件 : 字串必須全部由數字組成。 # age = int(input(‘>>>‘)) # print(age,type(age))  int ---> str    str(int) # s1 = str(123) # s2 = 123 # print(s1, type(s1)) # print(s2, type(s2))

 

                          bool <---> int         bool ---> int         True --->1    False ---> 0# print(int(True))# print(int(False))  int  ---> bool      非零即為True, 零為False。# print(bool(100))# print(bool(-1))# print(bool(0))         str                         
  i = ‘python12期’

 

  索引                      從0開始計算 依次 0 . 1. 2. ....           從右往左 為 -1 -2 -3 .....
# i1 = i[0]# print(i1)# i2 = i[4]# print(i2)# i3 =i[-1]# print(i3)

  切片  

       (顧頭不顧腚)

# i4 = i[0:6]# print(i4)# i5 = i[:6]# print(i5)   python# i6 = i[1:4]# print(i6)   yth# i7 =i[5:]# print(i7)    n12期# i8 = i[:]# print(i8)    python12期

  步長 

# i9 = i[:5:2]     前面空是開頭 開頭到5 步長為2# print(i9)       pto# i10 = i[4::2]   中間空是結尾 從4到結尾 步長為2# print(i10)      o1期# i11 = i[-1:-5:-1]     從後往前 步長為1# print(i11)            期21n# i12 = i[-3:-1]       切片 從-3 到-2 (顧頭不顧尾)# print(i12)            12

     字串的常用操作方法

s = ‘laoNANhai’

**  capitalize 首字母大寫,其餘字母小寫。

# s1 = s.capitalize()# print(s1)           # Laonanhai

*    center      置中。第一個參數確定大小,*表示填充左右空餘部分

# s2 = s.center(15,"*")# print(s2)          #***laoNANhai***
 *** upper      全大寫       lower      全小寫
# s3 = s.upper()# s31 er()# print(s3)           LAONANHAI# print(s31)         laonanhai

  PS

#code = ‘QAdr‘.upper()#your_code = input(‘請輸入驗證碼,不區分大小寫:‘).upper()#if your_code == code:#print(‘驗證成功‘)

*** startswith : 判斷以什麼內容開頭,返回bool,可以切片,切片用,隔開

     endswith : 

 startswith(self, prefix, start=None, end=None) 

# s4 = s.startswith(‘l‘)# s41 = s.startswith(‘lao‘)# s42 = s.startswith(‘N‘, 3, 6)# print(s42)        True# print(s4)          True# print(s41)        False

*   swapcase : 大小寫翻轉

# s5 = s.swapcase()# print(s5)

*   title : 非字母隔開的每個單詞的首字母大寫

# ss = ‘gdsj wusir6taibai*ritian‘# s6 = ss.title()# print(s6)
***  index :通過元素找索引,可切片,找不到報錯。***  find :   通過元素找索引,可切片,找不到返回-1
# s7 = s.find(‘a‘)# s71 = s.find(‘A‘,2,)       2 從第二個開始找# s71 = s.find(‘Q‘,)# print(s71)# s72 = s.index(‘Q‘)# print(s72)# print(s71)# ss = ‘\talex\n‘# print(ss)
***  strip  去除前後端的空格,分行符號 \n,定位字元(tab鍵  \t)                     也可去除前後指定字元(必須按出現順序)
# ss = ‘ ablabsexsba‘# s9 = ss.strip(‘a‘)# s9 = ss.strip(‘abs‘)#lstrip()        從左邊開始去除   # rstrip()       從右邊開始去除# print(s9)

ps  防止使用者手滑輸入空格 或者其他空白字元

username = input(‘請輸入使用者名稱‘).strip()if username == ‘碗蓉‘:print(‘登入成功‘)
*** split     str ---> list                 預設以空格分割
# s = ‘wusir alex taibai‘# st = ‘wusir,alex,taibai‘# st1 = ‘QwusirQalexQtaibai‘# s10 = s.split()# print(s10)# s101 = st.split(‘,‘)# print(s101)# s102 = st1.split(‘Q‘,2)     2表示前兩個Q 來分割# print(s102)

***  join  在某些情況下, list  ---> str

# s = ‘alex‘# s11 = ‘+‘.join(s)# print(s11)# l = [‘wusir‘, ‘alex‘, ‘taibai‘]# s111 = ‘ ‘.join(l)# print(s111,type(s111))

**  repalce  替換指定字元 

     replace(self, old, new, count=None)

# s = ‘小粉嫩小粉嫩ghlasdfg小粉嫩‘# # s12 = s.replace(‘小粉嫩‘, ‘大鐵鎚‘)# s12 = s.replace(‘小粉嫩‘, ‘大鐵鎚‘,2)   數字2 表示只替換前兩個字元# print(s12)

  公用方法

s = ‘fdsajlskgjdsaf;jdskfsdaf‘

  len()             總個數

 

# print(len(s))

 

count (‘’)   計算某些元素出現的個數,可切片

 # c1 = s.count(‘f‘)   # print(c1)

format  格式化輸出 

三種方式

# msg = ‘我叫{},今年{},愛好{}‘.format(‘太白‘, ‘20‘, ‘girl‘)# print(msg)

 

# msg = ‘我叫{0},今年{1},愛好{2},我依然叫{0}‘.format(‘太白‘, ‘20‘, ‘girl‘)# print(msg)

 

# msg = ‘我叫{name},今年{age},愛好{hobby}‘.\# format(name=‘太白‘, hobby=‘girl‘, age=‘20‘)# print(msg)

 

判斷 語句

# name=‘jinxin123‘# print(name.isalnum()) #字串由字母或數字組成    True# print(name.isalpha()) #字串只由字母組成             False# print(name.isdigit()) #字串只由數字組成              False

PS

 

 

 

python 學習 D3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.