Python之路【第二篇】:Python基礎,python第二篇

來源:互聯網
上載者:User

Python之路【第二篇】:Python基礎,python第二篇
Python基礎對於Python,一切事物都是對象,對象基於類建立

所以,以下這些值都時對象:"zhurui"、22、['北京','上海','深圳'],並且是根據不同的類產生的對象。

 

一、基礎資料型別 (Elementary Data Type)數字(int)

如:21、45、56

每一個整數都具備如下功能:

 

 1 - int 2  將字串轉換為數字 3 例子: 4 a = "123" 5 print(type(a),a) 6 輸出結果: 7 >>> a = "123" 8 >>> print(type(a),a) 9 <class 'str'> 12310 11 b = int(a)12 print(type(b),b)13 14 輸出結果:15 >>> b = int(a)16 >>> print(type(b),b)17 <class 'int'> 12318 19 num = "0022"20 v = int(num, base=16)21 print(v)22 23 輸出結果:24 >>> num = "0022"25 >>> v = int(num, base=16)26 >>> print(v)27 3428 29 - bit_length30  #當前數位二進位,至少用n位表示31 age = 2232 v = age.bit_length()33 print(v)34 35 輸出結果:36 >>> age = 2237 >>> v = age.bit_length()38 >>> print(v)39 5
字串(str)

1、name.capitalize()   #首字母大寫

例子:

>>> test = "zhUrui">>> v = test.capitalize()>>> print(v)Zhurui

 

2、name.casefold()   #所有變小寫,casefold更牛逼,很多未知的對相應變小寫

例子:

>>> test = "zhUrui">>> v1 = test.casefold()>>> print(v1)zhurui>>> v2 = test.lower()>>> print(v2)zhurui

 

3、name.center()  #設定寬度,並將內容置中 

   name.ljust()   #設定寬度,變數向左,其他部分用所定義的填充符 填充

   name.rjust()  #設定寬度,變數向右,其他部分用所定義的填充符 填充

      name.zfill()  #設定寬度,預設變數向右,其他部分用zfill方法特定的填充符"000" 填充

>>> test = "zhurui">>> v = test.center(20,"中")>>> print(v)中中中中中中中zhurui中中中中中中中解釋:#  20   代指總長度#   *    空白未知填充,一個字元,可有可無輸出結果:中中中中中中中zhurui中中中中中中中##########################################>>> test = "zhurui">>> v = test.ljust(20,"*") #ljust 變數靠左,其他部分用*填充>>> print(v)zhurui**************##################################>>> test = "zhurui">>> v = test.rjust(20,"*") #rjust 變數靠右,其他部分用*填充>>> print(v)**************zhurui
>>> test = "zhurui">>> v = test.zfill(20)  ##zfill只能用於000填充>>> print(v)00000000000000zhurui

 

 4、name.count()   #去字串中尋找,尋找子序列的出現次數

>>> test = "Zhuruizhuruiru">>> v = test.count('ru')>>> print(v)3>>> v = test.count('z')>>> print(v)1#########################################>>> test = "Zhuruizhuruiru">>> v = test.count('ru', 5, 6)>>> print(v)0

 

5、name.encode()  #將字串編碼成bytes格式

6、name.decode()

 

7、name.endswith("ui")  #判斷字串是否以ui結尾

   name.startswith('ui')  #判斷字串是否以ui開始

>>> test = "zhurui">>> v = test.endswith('ui')>>> print(v)True>>> v = test.startswith('ui')>>> print(v)False

 

 8、"Zhu\tRui".expandtabs(10)  #輸出‘Zhu          Rui’, 將\t轉換為多長的空格

>>> test = "Zhu\tRui">>> v = test.expandtabs(10)>>> print(v)Zhu       Rui################################test = "username\tpassword\temail\nzhurui\t123456\t24731701@qq.com\nzhurui\t123456\t24731701@qq.com\nzhurui\t123456\t24731701@qq.com"v = test.expandtabs(20)print(v)輸出結果:C:\Python35\python3.exe C:/Users/ZR/PycharmProjects/python全棧開發/day1/logging.pyusername            password            emailzhurui              123456              24731701@qq.comzhurui              123456              24731701@qq.comzhurui              123456              24731701@qq.com

 9、name.find(A)  #從開始往後找,找到第一個之後,擷取其位置即索引,找不到返回-1

## > 或 >=
# 未找到 -1
>>> test = "williamwilliam">>> v = test.find('am')>>> print(v)5>>> v = test.find('t') #找變數中的"t"字元,>>> print(v)-1 ##沒有找到,返回負一

 

10、name.index('a')  #找不到,報錯

>>> test = "williamwilliam">>> v = test.index('a')>>> print(v)5>>> v = test.index('8')  ##找字串中是否Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: substring not found

 

11、name.format()  #格式化,將一個字串中的預留位置替換為指定的值

>>> test = 'i am {name}, age {a}'>>> print(test)i am {name}, age {a}>>> v = test.format(name='william',a=22)>>> print(v)i am william, age 22

 

 

>>> test = 'i am {0},age {1}'>>> print(test)i am {0},age {1}>>> v = test.format('william', 22)>>> print(v)i am william,age 22

 

 12、name.format_map() #格式化, 傳入的值{"name":  'william',  "a":  22}

>>> test = 'i am {name}, age {a}'>>> v1 = test.format(name='zhurui',a=22)>>> v2 = test.format_map({"name":'zhurui', "a": 19})>>> print(v1)i am zhurui, age 22>>> print(v2)i am zhurui, age 19

 13、name.isalnum() #字串中是否只包含  字母和數字

>>> test = "234">>> v = test.isalnum()>>> print(v)True

 

14、name.isalpha() #是否是字母,漢字

>>> test = "asfdge242">>> v = test.isalpha()>>> print(v)False>>> test = "威廉">>> v = test.isalpha()>>> print(v)True

 15、判斷輸入的是否是數字

>>> test = "二" # 1 , ②>>> v1 = test.isdecimal   ##判斷十進位小數>>> v2 = test.isdigit()>>> v3 = test.isnumeric()  ##判斷漢語的數字,比如 "二">>> print(v1,v2,v3)<built-in method isdecimal of str object at 0x00000201FE440AB0> False True

 

 16、name.isprintable() #判斷是否存在不可顯示的字元

\t  定位字元
\n 換行
>>> test = "qepoetewt\tfdfde">>> v = test.isprintable()>>> print(v)False>>> test = "qepoetewtfdfde">>> v = test.isprintable()>>> print(v)True

 

 17、name.isspace() #判斷是否全部是空格

>>> test = "">>> v = test.isspace()>>> print(v)False>>> test = " ">>> v = test.isspace()>>> print(v)True

 

18、name.istitle() #判斷是否是標題,其中必須首字母大寫

>>> test = "Return True if all cased characters in S are uppercase">>> v1 = test.istitle()>>> print(v1)False>>> v2 = test.title()  #將字串首字母大寫>>> print(v2)Return True If All Cased Characters In S Are Uppercase>>> v3= v2.istitle()>>> print(v3)True

 

19、***** name.join() #將字串中的每一個元素按照指定分隔字元進行拼接(五星重點參數)

>>> test = "出任CEO迎娶白富美">>> print(test)出任CEO迎娶白富美>>> v = '_'.join(test)>>> print(v)出_任_C_E_O_迎_娶_白_富_美>>>

 

20、name.islower() #判斷是否全部是大小寫 和 轉換為大小寫

>>> test = "William">>> v1 =test.islower() #判斷是否全都是小寫>>> v2 = test.lower()  #將變數轉換為小寫>>> print(v1, v2)False william>>>###################################>>> test = "William">>> v1 =test.isupper() #判斷是否全都是大寫>>> v2 = test.upper()  #將變數轉換為大寫>>> print(v1, v2)False WILLIAM

 

 21、移除指定字串,優先最多匹配

>>> test = 'xa'>>> v1 =test.isupper()>>> v = test.lstrip("xa")>>> print(v)>>> v = test.rstrip("92exxxexxa")>>> print(v)>>> v = test.strip("xa")>>> print(v)#################################### test.lstrip()# test.rstrip()# test.strip()# 去除左右空白# v = test.lstrip()# v = test.rstrip()# v = test.strip()# print(v)# print(test)# 去除\t \n# v = test.lstrip()# v = test.rstrip()# v = test.strip()# print(v)

 

22、對應關係替換

>>> test = "aeiou">>> test1 = "12345">>> v = "asidufkasd;fiuadkf;adfkjalsdjf">>> m = str.maketrans("aeiou", "12345")>>> new_v = v.translate(m)>>> print(new_v)1s3d5fk1sd;f351dkf;1dfkj1lsdjf

 

23、name.partition() #分割為三部分

>>> test = "testegerwerwegwewe">>> v = test.partition('s')>>> print(v)('te', 's', 'tegerwerwegwewe')>>> v = test.rpartition('s')>>> print(v)('te', 's', 'tegerwerwegwewe')>>>

 

24、name.split() #分格為指定個數

>>> test = "sagesgegessress">>> v = test.split('s',2)>>> print(v)['', 'age', 'gegessress']>>>

 

25、分割, 只能根據,true, false:是否保留換行

>>> test = "fwerwerdf\frweqnndasfq\fnaqewrwe">>> v = test.splitlines(False)>>> print(v)['fwerwerdf', 'rweqnndasfq', 'naqewrwe']

 

26、以xxx開頭,以xx結尾

>>> test = "backend 1.2.3.4">>> v = test.startswith('a')>>> print(v)False>>> test.endswith('a')False

 

27、name.swapcase() #大小寫轉換

>>> test = "WiiLiAm">>> v = test.swapcase()>>> print(v)wIIlIaM

 

28、name.isidentifier() #字母,數字,底線  :標識符  def  class

>>> a = "def">>> v = a.isidentifier()>>> print(v)True

 

29、name.replace() #將指定字串替換為指定字串,替換功能相當於sed

>>> test = "williamwilliamwilliam">>> v = test.replace("am", "bbb")>>> print(v)willibbbwillibbbwillibbb>>> v = test.replace("am", "bbb",2)>>> print(v)willibbbwillibbbwilliam>>>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.