一、編碼部分(結合執行結果進行理解)
name = "my \tname is and i am old"
print(name.capitalize())
print(name.center(50,"-"))
#列印50個字元,不夠的用-補齊,並將chenhl放在中間。
print(name.encode())
#將字串轉成二進位制,在python中0開頭表示8進製,0x或者0x表示16進製制,0b或者0b表示二進位制。
print(name.endswith("hl"))
#判斷字串以什麼結尾
print(name.expandtabs(tabsize=10))
#自動在字串中的tab鍵處補上空格
print(name.format(name='chenhl',year='30'))
print(name.format_map())
#格式化輸入,map 用於字典中
print(name.find("n"))
#獲取n的索引,即位置,字串可以切片
print(name.isalnum())
#判斷變數是否是阿拉伯數字+阿拉伯字元,包含英文本元和0---9數字。是返回true,非則相反。
print('name'.isalpha())
#判斷變數是否是純英文本元,是返回true,非則相反。
print(name.isdecimal())
print('1.23'.isdecimal())
print('11'.isdecimal())
print('1b'.isdecimal())
#判斷是否是十進位制
print(name.isdigit())
#判斷是不是整數
print('name'.isidentifier())
#判斷是不是乙個合法的識別符號
print(name.islower())
#判斷是不是小寫
print(name.isnumeric())
print('12'.isnumeric())
print('1.2'.isnumeric())
#判斷是不是只有數字
print('my name is'.istitle())
print('my name is'.istitle())
#判斷是不是首字母大寫
print('my name is '.isprintable())
#判斷是不是可以列印的檔案,tty的檔案會返回非。
print('my future'.isupper())
#判斷是不是都是大寫
print(','.join(['1','2','3']))
#將列表的內容轉成字串,或將join內容當成一條指令交給os執行。
print(name.ljust(50,''))
#保證字串的長度,不夠用在右側補齊。
print(name.rjust(50,'-'))
#保證字串長度,不夠用-在左側補齊。
print('abcd'.lower())
#把大寫變成小寫
print('abcd'.upper())
#把小寫變成大寫
print(' abcd'.lstrip())
print('\nabcd'.lstrip())
#去掉左側的空格或回車。rstrip,去掉右側的空格或回車,strip去掉兩側的。
p = str.maketrans('abcdef','123456')
print('chenhl'.translate(p))
#將字串換成後面數字對應的值
print('chenhl'.replace('h','h',1))
#將h替換成大寫h,count替換幾個
print('chenhl'.rfind('h'))
#找到值得最後乙個位置
print('ch en hl'.split())
print('ch en hl'.split('h'))
#把字串按照空格或固定字元轉成列表
print('che\nhl'.splitlines())
#把字串按照換行轉成列表,特別是不同系統的換行
print('chenhl'.startswith('c'))
#判斷以什麼開頭的字串
print('hello world'.swapcase())
#將大寫的首字母轉換成小寫
print('hello world'.title())
#將首字母變成大寫
print('hello world'.zfill(20))
#用零補位,
二、執行結果
my name is and i am old
------my name is and i am old------
b'my \tname is and i am old'
false
my name is and i am old
my name is chenhl and i am 30 old
my name is chenhl and i am 20 old
4false
true
false
false
true
false
false
true
true
false
true
false
false
true
true
false
1,2,3
my name is and i am old****
------------my name is and i am old
abcd
abcd
abcd
abcd
3h5nhl
chenhl
4['ch', 'en', 'hl']
['c', ' en ', 'l']
['che', 'hl']
true
hello world
hello world
000000000hello world
python3 字串方法
1 join方法 test nancy v join test print v 執行結果 n a n c y 2 split方法 不帶引數時,字串以空格 n t進行分割 例子 test1 nancy alex tim t james nfigo v1 test1.split print v1 執行結...
python3字串方法總結
1 capitalize 將字串的第乙個字元轉換為大寫 2center width,fillchar 返回乙個指定的寬度 width 居中的字串,fillchar 為填充的字元,預設為空格。3count str,beg 0,end len string 返回 str 在 string 裡面出現的次數...
Python 3 字串 rsplit 方法
str.rsplit sep none,maxsplit 1 a 我 愛 yi 條 chai a.rsplit 與 split 無異 我 愛 yi 條 chai b wo 愛 u b.rsplit wo 愛 u c 我 愛 一 條 柴 c.rsplit 我 愛 一 條 柴 d d.rsplit e ...