python學習筆記字串(二)

來源:互聯網
上載者:User

標籤:python

字串類型(string)

字串是以單引號或雙引號"括起來的任意文本,比如‘abc‘"123"等等。

請注意,‘‘""本身只是一種表示方式,不是字串的一部分,因此,字串‘abc‘只有abc這3個字元。如果本身也是一個字元,那就可以用""括起來,比如"I‘m OK"包含的字元是Im,空格,OK這6個字元。

1、建立字串

b =   ‘asdasd‘

2、字串操作

a.重複輸出字串

print(‘Python‘ * 5)

>>>PythonPythonPythonPythonPython

b.通過索引擷取字串中字元,這裡和列表的切片操作是相同的,具體內容見列表

print(‘hello world‘[2:])

>>>llo world

c.in  成員運算子 - 如果字串中包含給定的字元返回 True

print(‘rl‘ in  ‘hello world‘)

>>>True

d.%格式字串

print("she sis a good girl")print("she sis a %s girl" % ‘good‘)

>>>she is a good girl

>>>she is a good girl

e.字串拼接a

a = ‘zxc‘b = ‘asd‘c = ‘qwe‘print(a+b+c)

>>>zxcasdqwe

以上方式效率極低,建議用join做字串拼接

a = ‘zxc‘b = ‘asd‘c = ‘qwe‘print(‘‘.join([a,b,c]))print(‘+‘.join([a,b,c]))

>>>zxcasdqwe

>>>zxc+asd+qwe

3、字串內建方法

capitalize把字串的第一個字元大寫,且不會修改原字串,返回一個新的字串
a = ‘hello world!‘print(a.capitalize())

>>>Hello world!


center返回一個原字串置中,並使用*填充至長度 20 的新字串
a = ‘asd‘print(a.center(20,‘*‘))

>>>********asd*********


count統計字串中元素的個數

a = ‘hello world!‘print(a.count(‘l‘))

>>>3


endswith判斷是否以某個內容結尾,返回布爾值

a = ‘hello world!‘print(a.endswith(‘ld!‘))

>>>True


startswith判斷是否以某個內容開始,返回布爾值


expandtabs(tabsize=8)把字串中的tab符號轉為空白格,tab預設空格數是8.

a = ‘hello\t worl\td!‘print(a.expandtabs(tabsize=10))

>>>hello     worl      d!


string.find(str, beg=0, end=len(string))檢測 str 是否包含在 string 中,如果 beg 和 end 指定範圍,則檢查是否包含在指定範圍內,如果是返回開始的索引值,否則返回-1
a = ‘hello world!‘print(a.find(‘or‘))print(a.find(‘or‘,5,9))print(a.find(‘or‘,2,5))

>>>7

>>>7

>>>-1


string.index(str, beg=0, end=len(string))跟find()方法一樣,只不過如果str不在 string中會報一個異常.
a = ‘hello world!‘print(a.index(‘or‘))print(a.index(‘or‘,5,9))print(a.index(‘or‘,1,3))

>>>7

>>>7

>>>Traceback (most recent call last):

      File "str.py", line 6, in <module>

        print(a.index(‘or‘,1,3))

>>>ValueError: substring not found


string.isalnum()如果string所有字元都是字母或數字則返回 True,否則返回 False
a = ‘helloworld‘b = ‘123123‘c = ‘python3‘d = ‘python 3‘print(a.isalnum())print(b.isalnum())print(c.isalnum())print(d.isalnum())

>>>True

>>>True

>>>True

>>>False


string.isalpha()如果 string 所有字元都是字母則返回 True,否則返回 False
a = ‘helloworld‘c = ‘python3‘print(a.isalpha())print(c.isalpha())

>>>True

>>>False


string.isdecimal()如果 string 只包含十進位數字則返回 True 否則返回 False.
string.isdigit()如果 string 只包含數字則返回 True 否則返回 False.
string.islower()如果 string 中包含至少一個區分大小寫字元,並且所有這些(區分大小寫)字元都是小寫,則返回 True,否則返回 False
string.isnumeric()如果 string 中只包含數字字元,則返回 True,否則返回 FalseTrue: Unicode數字,byte數字(單位元組),全形數字(雙位元組),羅馬數字False: 漢字數字Error: 無True: Unicode數字,,全形數字(雙位元組)False: 羅馬數字,漢字數字Error: byte數字(單位元組)True: Unicode數字,全形數字(雙位元組),羅馬數字,漢字數字False: 無Error: byte數字(單位元組)string.

 string.istitle()如果 string 是標題化的(見 title())則返回 True,否則返回 False

a = ‘hello world‘b = ‘Hello World‘print(a.istitle())print(b.istitle())

>>>False

>>>True


string.isupper()如果 string 中包含至少一個區分大小寫字元,並且所有這些(區分大小寫)字元都是大寫,則返回 True,否則返回 False


string.join(seq)以 string 作為分隔字元,將 seq 中所有的元素(的字串表示)合并為一個新的字串

 = b = (.join())(.join([b]))

>>>M+Y+ +G+I+R+L

>>>MY GIRL$xxxx


string.ljust(width)返回一個原字串靠左對齊,並使用空格填充至長度 width 的新字串

 = (.ljust())

>>>MY GIRL-------------


string.lower()轉換 string 中所有大寫字元為小寫.


string.lstrip()截掉 string 左邊的空格


string.maketrans(intab, outtab])用於建立字元對應表的轉換表,對於接受兩個參數的最簡單的調用方式,第一個參數是字串,表示需要轉換的字元,第二個參數也是字串表示轉換的目標。

 = (.maketrans())

>>>{120: 98}


max(str)返回字串 str 中最大的字母。


min(str)返回字串 str 中最小的字母。


string.partition(str)有點像 find()和 split()的結合體,從 str 出現的第一個位置起,把 字 符 串 string 分 成 一 個 3 元 素 的 元 組 (string_pre_str,str,string_post_str),如果 string 中不包含str 則 string_pre_str == string.


string.replace(str1, str2,  num=string.count(str1))  把 string 中的 str1 替換成 str2,如果 num 指定,則替換不超過 num 次.


string.rfind(str, beg=0,end=len(string) )類似於 find()函數,不過是從右邊開始尋找.


string.rindex( str, beg=0,end=len(string))類似於 index(),不過是從右邊開始.


string.rjust(width)返回一個原字串靠右對齊,並使用空格填充至長度 width 的新字串


string.rpartition(str)類似於 partition()函數,不過是從右邊開始尋找.


string.rstrip()刪除 string 字串末尾的空格.


string.split(str="", num=string.count(str))以 str 為分隔字元切片 string,如果 num有指定值,則僅分隔 num 個子字串


# string.splitlines(num=string.count(‘\n‘))            按照行分隔,返回一個包含各行作為元素的列表,如果 num 指定則僅切片 num 個行.

# string.startswith(obj, beg=0,end=len(string))        檢查字串是否是以 obj 開頭,是則返回 True,否則返回 False。如果beg 和 end 指定值,則在指定範圍內檢查.

# string.strip([obj])                                  在 string 上執行 lstrip()和 rstrip()

# string.swapcase()                                    翻轉 string 中的大小寫

# string.title()                                       返回"標題化"的 string,就是說所有單詞都是以大寫開始,其餘字母均為小寫(見 istitle())

# string.translate(str, del="")                        根據 str 給出的表(包含 256 個字元)轉換 string 的字元,要過濾掉的字元放到 del 參數中

# string.upper()                                       轉換 string 中的小寫字母為大寫


python學習筆記字串(二)

聯繫我們

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