(1)字元串常用方法:
1、upper(self) ---->将字元串中所有字母大寫,不去管其它非字母字元。
參數:無
傳回值:字元串
str1 = 'hello my name is 張'
print(str1.upper())
輸出:
HELLO MY NAME IS 張
2、lower(self) ---->把全部字母字元轉換成小寫,不去管其它非字母字元。
參數:無
傳回值:字元串
str1 = 'Zbuter'
print(str1.lower())
輸出:
zbuter
3、swapcase(self) ---->将字元串中的大小寫字元互換
參數:無
傳回值:字元串
str1 = '張Zbuter'
print(str1.swapcase())
輸出:
張zBUTER
4、count(self, sub, start=None, end=None) ---->計算字元串中從 start 開始到 end 結束 sub 出現的次數 start 預設是字元串開始 end 預設是字元串結束
參數:
sub:字元或字元串
start:開始的索引位置
end:結束的索引位置
傳回值:字元串中sub出現的次數
hello = 'hello world'
print(hello.count('o'))
print(hello.count('o',6))
輸出:
2
1
5、capitalize(self) ---->将字元串的首字母大寫
參數:無
傳回值:将字元串的首字母大寫的字元串
name = 'zjs'
print(name.capitalize())
輸出:
Zjs
6、casefold(self) 與 lower(self) ---->将字元串小寫
參數:無
傳回值:将字元串小寫的字元串
name = 'ZJS'
print(name.lower())
print(name.casefold())
輸出:
zjs
zjs
casefold(self) 與 lower(self)的差別是:
lower() 隻對 ASCII 也就是 'A-Z'有效,但是其它一些語言裡面存在小寫的情況就沒辦法了。文檔裡面舉得例子是德語中'ß'的小寫是'ss'(這個我也不懂)
7、center(self, width, fillchar=None) ---->将字元串居中用fillchar填充 一共width個字元 預設是使用空格填充 傳回新的字元串
參數:
width:新形成的字元串長度,如果源字元串比width指定的長度長則直接傳回源字元串
fillchar:指定一個字元來填充
傳回值:填充後的字元串
name = 'ZJS'
print(name.center(20))
print(name.center(20, '-'))
輸出:
ZJS
--------ZJS---------
8、ljust(self, width, fillchar=None) ---->傳回一個長度為width,左對齊的字元串,最右邊填充fillchar,預設為空格。width要大于len(str),否則傳回原字元串。
str1 = 'Zbuter'
print(str1.ljust(20))
print(str1.ljust(20, '-'))
輸出:
Zbuter
Zbuter--------------
9、rjust(self, width, fillchar=None) ---->與ljust類似 右對齊
10、lstrip(self, chars=None) ---->傳回一個去除前導字元的新字元串,chars參數是一個字元串,它包含了所有将要被移除的字元集合。預設為空格。
參數:
chars:字元集合
傳回值:字元串
str1 = ' www.zbuter.cn'
str2 = 'www.zbuter.cn'
print(str1.lstrip())
print(str2.lstrip('cnw.'))
輸出:
www.zbuter.cn
zbuter.cn
11、rstrip(self, chars=None) ---->與lsplit類似 從右側開始
12、strip(self, chars=None) ---->與lstrip和rstrip類似 在字元串兩頭開始比對。多用于清除字元串兩端的空格
13、endswith(self, suffix, start=None, end=None) ---->判斷字元串是否以 suffix 結尾 start 預設是字元串開始 end 預設是字元串結束
參數:
suffix:字尾
start:開始的索引位置
end:結束的索引位置
傳回值:布爾值
hello = 'hello world'
print(hello.endswith('ld'))
print(hello.endswith('asd'))
輸出:
True
False
14、startswith(self, prefix, start=None, end=None) ---->與endswith類似 判斷是否以prefix開頭
15、expandtabs(self, tabsize=8) ---->把字元串中的 tab 符号('\t')轉為空格,tab 符号預設的空格數是 8。從頭開始數,數到第一個\t正好為8個空格,不足則補空格,如果還有\t,接着從第一個\t數到第二個\t仍然為8個空格,以此類推直到最後一個\t結束。
參數:
tabsize:指定轉換字元串中的 tab 符号('\t')轉為空格的字元數,預設的字元數是8。
傳回值:該方法傳回字元串中的 tab 符号('\t')轉為空格後生成的新字元串。
hello = 'hello\tworld'
print(hello.expandtabs(4))
print(hello.expandtabs(10))
輸出:
hello world
hello world
16、find(self, sub, start=None, end=None) ---->在字元串中查找 sub 出現的位置
參數:
sub:指定查找的字元串或字元
start:開始的索引位置
end:結束的索引位置
傳回值:sub出現的第一次的索引 如果不存在則傳回-1
hello = 'hello\tworld'
print(hello.find('o'))
print(hello.find('z'))
輸出:
4
-1
17、rfind(self, sub, start=None, end=None) ---->與find類似 從右側查找
18、index(self, sub, start=None, end=None) ---->在字元串中查找 sub 出現的位置
參數:
sub:指定查找的字元串或字元
start:開始的索引位置
end:結束的索引位置
傳回值:sub出現的第一次的索引 如果不存在則抛出異常
hello = 'hello\tworld'
print(hello.index('o'))
print(hello.index('z'))
輸出:
4
Traceback (most recent call last):
File "E:/zjs/Python/pythonTest/day01/day01/d1.py", line 36, in
print(hello.index('z'))
ValueError: substring not found
19、rindex(self, sub, start=None, end=None) ---->與index類似 從右側查找
20、title(self) ---->将字元串中所有單詞的首字母大寫
參數:無
傳回值:字元串
str1 = 'hello my name is 張'
print(str1.title())
輸出:
Hello My Name Is 張
21、replace(self, old, new, count=None) ---->用 new 替換原字元串中的 old ,count指定替換的次數
參數:
old:需要替換的字元(串)
new:替換的字元(串)
count:替換的次數不超過count次
傳回值:字元串
str1 = 'www.zbuter.cn'
str2 = 'www.zbuter.cn'
print(str1.replace('.', '。'))
print(str2.replace('.', ',', 1))
輸出:
www。zbuter。cn
www,zbuter.cn
22、partition(self, sep) ---->該方法用于拆分字元串,傳回一個包含三個元素的元組。如果未能在原字元串中找到Sep,則元組的三個元素為:原字元串,空串,空串;否則,從原字元串中遇到的第一個Sep字元開始拆分,元組的三個元素為:Sep之前的字元串,Sep字元,Sep之後的字元串;
參數:
sep:分割标記
傳回值:元祖
str1 = ' www.zbuter.cn'
str2 = 'www.zbuter.cn'
print(str1.partition('.'))
print(str2.partition('z'))
輸出:
(' www', '.', 'zbuter.cn')
('www.', 'z', 'buter.cn')
23、rpartition(self, sep) ---->與partition類似 從右側查找sep
24、split(self, sep=None, maxsplit=-1) ---->傳回一個以Sep分隔的清單,maxsplit指定拆分次數(是以,清單中元素的個數為maxsplit + 1)。Sep預設為空格,maxsplit預設不限制拆分次數。
參數:
sep:分隔符
maxsplit:拆分次數
傳回值:字元串
str1 = 'www.zbuter.cn'
str2 = 'www.zbuter.cn'
print(str1.split('.'))
print(str2.split('.', 1))
輸出:
['www', 'zbuter', 'cn']
['www', 'zbuter.cn']
25、rsplit(self, sep=None, maxsplit=-1) ---->與 split 類似 從右側拆分
26、splitlines(self, keepends=None) ---->拆分一個包含多行的字元串,以每行為一個元素傳回一個清單 keepends是一個True字元或非零整數
參數:
keepends:是否在清單内也添加換行符
傳回值:字元串
str1 = '123\n456'
str2 = '123\n456'
print(str1.splitlines())
print(str2.splitlines(True))
輸出:
['123', '456']
['123\n', '456']
27、join(self, iterable) ---->使用連接配接符str來連接配接iterable對象中的元素,如果傳入一個非iterable對象,如整數、布爾值等,将抛出異常Type Error。
參數:iterable:使用字元串來連接配接iterable
傳回值:字元串
str1 = 'Zbuter'
print(str1.join('12'))
print(str1.join('123'))
print(str1.join(123))
輸出:
1Zbuter2
1Zbuter2Zbuter3
Traceback (most recent call last):
File "E:/zjs/Python/pythonTest/day01/day01/d1.py", line 39, in
print(str1.join(123))
TypeError: can only join an iterable
28、zfill(self, width) ---->傳回一個長度為width的數字字元串,最左邊填充0。如果width小于等于原字元串長度,則傳回原字元串。主要用于數字類字元串的格式化。
參數:
width:填充的寬度
傳回值:字元串
str1 = 'aaa'
str2 = '123'
print(str1.zfill(5))
print(str2.zfill(5))
輸出:
00aaa
00123
29、format(self, *args, **kwargs) ---->格式化字元串
參數:
*args:是用來發送一個(非鍵值對)可變數量的參數清單給一個函數
**kwargs 允許你将不定長度的鍵值對, 作為參數傳遞給一個函數。 如果你想要在一個函數裡處理帶名字的參數, 你應該使用**kwargs。
傳回值:格式化後的新字元串
str1 = 'my name is {name} age is {age}'
str2 = 'my name is {0} age is {1}'
print(str1.format(name = 'zjs', age = 12))
print(str2.format('zjs', 12))
輸出:
my name is zjs age is 12
my name is zjs age is 12
更多參考:http://www.cnblogs.com/wupeiqi/articles/5484747.html
30、format_map(self, mapping) ----> 待更新
31、isalnum(self) ---->字元串中是否隻含數字、字母
參數:無
傳回值:布爾值
str1 = '123abc'
str2 = '123_abc'
print(str1.isalnum())
print(str2.isalnum())
輸出:
True
False
32、isalpha(self) ---->字元串是否隻含有字母
參數:無
傳回值:布爾值
str1 = 'abcdef'
str2 = '_abc'
print(str1.isalpha())
print(str2.isalpha())
輸出:
True
False
33、isdecimal(self) ---->字元串是否隻包含十進制字元。這種方法隻存在于unicode對象。
參數:無
傳回值:布爾值
str1 = '123'
str2 = 'this123'
print(str1.isdecimal())
print(str2.isdecimal())
輸出:
True
False
34、isdigit(self) ---->檢測字元串是否隻由數字組成。
參數:無
傳回值:布爾值
str1 = '123'
str2 = 'this123'
print(str1.isdigit())
print(str2.isdigit())
輸出:
True
False
35、isidentifier(self) ---->判斷字元串是否是合法的辨別符
參數:無
傳回值:布爾值
str1 = '_a'
str2 = '1a'
print(str1.isidentifier())
print(str2.isidentifier())
輸出:
True
False
36、islower(self) ---->判斷字元串是否全是小寫
參數:無
傳回值:布爾值
str1 = 'abcdefg'
str2 = 'Abcdefg'
print(str1.islower())
print(str2.islower())
輸出:
True
False
37、isnumeric(self) ---->判斷字元串是否隻包含數字字元。數字字元範圍很大,一般來說,數字字元是擁有如下屬性值的字元:Numeric_Type=Digit, Numeric_Type=Decimal或Numeric_Type=Numeric。比較isdecimal()、isdigit()、isnumeric(),幾個方法檢測的範圍依次擴大。
參數:無
傳回值:布爾值
str1 = '123'
str2 = 'a123'
print(str1.isnumeric())
print(str2.isnumeric())
輸出:
True
False