一、字符串具有不可改變的特性

>>>?str1?=?'your'
>>>?str1?=?str1?+?'name'
>>>?str1
'yourname'

國二python難嗎。當執行第二行的時候,在內存里又開辟了一個空間存儲"yourname",而不是在原來存儲“your”的地方修改,使得str1指向這個新地址


二、切片

>>>?str='my?name?is?sam,?and?i?love?girl'
>>>?str[1]
'y'
>>>?str[3:7]
'name'
>>>?str[-4:]????#截取最后4個字符
'girl'
>>>?str[-1:-5:-1]????#反序截取
'lrig'


三、字符串長度

>>>?str='my?name?is?sam,?and?i?love?girl'
>>>?len(str)
31


四、包含

>>>?str='my?name?is?sam,?and?i?love?girl'??
>>>?'name'?in?str???????#??不存在則報錯
True
>>>?'tiger'?not?in?str
True


五、循環

>>>?str='your?name'
>>>?for?i?in?str:
...?????print(i)
...
y
o
u
r
n
a
m
e


六、字符串方法

1. count

>>>?'your?name,?ming?zi'.count('n')
2
>>>?'your?name,?ming?zi'.count('n',6)
1
>>>?'your?name,?ming?zi'.count('n',6,8)
0

2.?capitalize

>>>?"youy?name".capitalize()
'Youy?name'

3. ?center

>>>?"youy?name".center(50,'-')
'--------------------youy?name---------------------'


4. encode

>>>?"youy?name".encode(encoding='utf-8')
b'youy?name'


5. endswith

>>>?"youy?name".endswith("me")
True


6.?expandtabs

>>>?"\tyouy?name".expandtabs(10)
'??????????youy?name'


7. format

>>>?"I?am?%s,?%d?year?old,?I?love?%s"?%?('Sam',12,?'dog')
'I?am?Sam,?12?year?old,?I?love?dog'
>>>?"I?am?{},?{}?year?old,?I?love?{}".format('Sam',12,?'dog')
'I?am?Sam,?12?year?old,?I?love?dog'
>>>?"I?am?{2},?{0}?year?old,?I?love?{1}".format(12,'dog',?'Sam')
'I?am?Sam,?12?year?old,?I?love?dog'
>>>?"I?am?{name},?{age}?year?old,?I?love?{dong}".format(name='Sam',age=12,?dong='dog')
'I?am?Sam,?12?year?old,?I?love?dog'

注:%s字符串,%d整型,%f浮點數


8. format_map

>>>?'My?name?is?{name},?I?am?{age}?year?old'.format_map({'name':?'Sam',?'age':?23})
'My?name?is?Sam,?I?am?23?year?old'


9. find ?左邊找 ? ?rfind 右邊找

>>>?'My?name?is?Sam,?I?am?23?year?old'.find('a')??#?左邊找
4
>>>?'My?name?is?Sam,?I?am?23?year?old'.find('a',5)
12
>>>?'My?name?is?Sam,?I?am?23?year?old'.find('a',13,?19)
18
>>>?'yui'.find('l')??#?找不到則返回-1
-1>>>?'My?name?is?Sam,?I?am?23?year?old'.rfind('a')??#?右邊找
26


10. index

>>>?'My?name?is?Sam,?I?am?23?year?old'.index('a')
4
>>>?'My?name?is?Sam,?I?am?23?year?old'.index('a',5)
12
>>>?'My?name?is?Sam,?I?am?23?year?old'.index('a',?13,?19)
18
>>>?'yui'.index('l')??#?找不到則,報錯
Traceback?(most?recent?call?last):File?"<stdin>",?line?1,?in?<module>
ValueError:?substring?not?found


11.?isalnum?檢查判斷字符串是否包含字母數字字符

>>>?'name'.isalnum()
True
>>>?'12'.isalnum()
True
>>>?'AAB'.isalnum()
True
>>>?'!fff'.isalnum()
False


12.?isalpha ?檢測字符串是否只由字母組成

>>>?'asdf'.isalpha()
True
>>>?'1234'.isalpha()
False
>>>?'gh67'.isalpha()
False


13.?isdecimal ?非負整數?檢查字符串是否只包含十進制字符。這種方法只存在于unicode對象。

>>>?'ghjjj'.isdecimal()
False
>>>?'12'.isdecimal()
True
>>>?'12.7'.isdecimal()
False

?

14.?isdigit ? 非負整數 ? ? 檢測字符串是否以數字組成

>>>?'12'.isdigit()
True
>>>?'12.8'.isdigit()
False
>>>?'-12'.isdigit()
False
>>>?'0'.isdigit()
True


15. ?isidentifier ?檢測字符串是否以字母開頭

>>>?'hhh'.isidentifier()
True
>>>?'77'.isidentifier()
False
>>>?'!@'.isidentifier()
False
>>>?'7ggg'.isidentifier()
False


16.?islower

>>>?'AhhBgggG'.islower()
False
>>>?'hhhhh'.islower()
True


17. ?isnumeric ? ?檢測字符串是否只由數字組成。這種方法是只針對unicode對象。

>>>?'678'.isnumeric()
True
>>>?'678.9'.isnumeric()
False
>>>?'678.0'.isnumeric()
False


18. ?isprintable ? ?判斷字符串中所有字符是否都屬于可見字符

>>>?'hyyuuuu\nyyyyyy'.isprintable()
False
>>>?'hyyuuuuyyyyyy'.isprintable()
True


19. ?isspace ? ? 檢測字符串是否為空格

>>>?'jjjiiii'.isspace()
False
>>>?'?'.isspace()
True


20. ?istitle ? ?判斷字符串是否適合當作標題(其實就是每個單詞首字母大寫)

>>>?'Your?Name'.istitle()
True


21. ?isupper ? ?判斷字符串中所有字母字符是否都是大寫字母

>>>?'fghhh'.isupper()
False
>>>?'FGGGGG'.isupper()
True


22. ?join ? 字符串列表連接

>>>?'+'.join('string')
's+t+r+i+n+g'
>>>?'+'.join(['i',?'am',?'your'])
'i+am+your'


23. ?strip lstrip rstrip ?移除左右 ?空格 ?換行符

str?=?'??ggggggggggg????\n'
print(str)
print(str.strip())
print(str.lstrip())
print(str.rstrip())
print('````````````')


24. ljust ?rjust ? 與center 類似

>>>?'name'.ljust(50,?'*')
'name**********************************************'
>>>?'name'.rjust(50,?'*')
'**********************************************name'


25. lower ? 將字符串的大寫換成小寫

>>>?'ASDF'.lower()
'asdf'
>>>?'AS7766!DF'.lower()
'as7766!df'


26. ?maketrans ?translate

>>>?'i?am?your?hero?sb'.translate({'abc':?'123'})
'i?am?your?hero?sb'
>>>?tab?=?'i?your?hero?sb'.maketrans('abc','123')
>>>?'i?am?your?hero?sb'.translate(tab)
'i?1m?your?hero?s2'




27.?partition ? rpartition ?如果分隔符包含在字符串中,返回一個元組,第一個為分隔符左邊字符串,第二個為分隔符,第三個為右邊字符串

>>>?'zxcvbnvbhjuu'.partition('vb')??#?匹配左邊的
('zxc',?'vb',?'nvbhjuu')
>>>?'zxcvbnvbhjuu'.partition('pp')
('zxcvbnvbhjuu',?'',?'')
>>>?'zxcvbnvbhjuu'.rpartition('vb')?#?匹配右邊的
('zxcvbn',?'vb',?'hjuu')
>>>?'zxcvbnvbhjuu'.rpartition('pp')
('',?'',?'zxcvbnvbhjuu')


28. ?replace

>>>?'your?name,?i?am?a?boy'.replace('a',?'B')
'your?nBme,?i?Bm?B?boy'
>>>?'your?name,?i?am?a?boy'.replace('a',?'B',?1)
'your?nBme,?i?am?a?boy'


29. split ?rsplit

>>>?'your?name,?i?am?a?boy'.split('a')
['your?n',?'me,?i?',?'m?',?'?boy']
>>>?'your?name,?i?am?a?boy'.split('a',2)??#?從左邊劃分
['your?n',?'me,?i?',?'m?a?boy']
>>>?'your?name,?i?am?a?boy'.rsplit('a',2)??#?從右邊劃分
['your?name,?i?',?'m?',?'?boy']


30. ?splitlines

>>>?'''My?name\n?is?Sam,?I?am\n?23?year?old'''.splitlines()
['My?name',?'?is?Sam,?I?am',?'?23?year?old']


31. ?startswith

>>>?'My?name?is?Sam,?I?am?23?year?old'.startswith('My',4,10)
False
>>>?'My?name?is?Sam,?I?am?23?year?old'.startswith('me',4,10)
False
>>>?'My?name?is?Sam,?I?am?23?year?old'.startswith('ame',4,10)
True


32. ?swapcase

>>>?'ASfrtAAggg'.swapcase()
'asFRTaaGGG'


33. title

>>>?'you?name'.title()
'You?Name'

??
34. upper

>>>?'i?am?your?hero?sb'.upper()
'I?AM?YOUR?HERO?SB'


35. ?zfill

>>>?"youy?name".zfill(50)
'00000000000000000000000000000000000000000youy?name'