Python字符串
有編程基礎的人對字符串並不陌生,在C++中定義一個字符需要帶上數據類型如:
string str=“Hello”
而在Python中定義一個字符串很方便
str = ‘Hello’ #使用單引號
str = “Hello” #使用雙引號
Python字符串使用方法:
1 capitalize()方法,將字符串首字母大寫
str='hello word!' test = str.capitalize() print(test)輸出:
Hello word!
2 casefold()方法,將字符串轉換爲小寫
str='HELLO word!' test = str.casefold() print(test)輸出:hello word!
3 center(self, width, fillchar=None)方法,使用fillchar填充到原字符串的兩端,直至長度爲width
str='HELLO word!' test = str.center(12,'a') print(test)輸出:HELLO word!a
str='HELLO word!' test = str.center(13,'a') print(test)輸出:aHELLO word!a
4 count(self, sub, start=None, end=None)方法,統計sub在字符串中出現的次數,可以指定起始位置與結束位置
str='abbcccddddeeeee' test = str.count('c') print(test)輸出:3
str='abbcccddddeeeee' test = str.count('c',0,4) print(test)輸出:1
str='abbcccddddeeeee' test = str.count('ee') print(test)輸出:2
5 encode(encoding='UTF-8',errors='strict')方法以 encoding 指定的編碼格式編碼字符串。errors參數可以指定不同的錯誤處理方案。
6 endswith(self, suffix, start=None, end=None)方法,判斷字符串是否以suffix結尾,可以指定起始位置與結束位置
str='abbcccddddeeeee' test = str.endswith('e') print(test)
str='abbcccddddeeeee' test = str.endswith('e',0,6) print(test)輸出:FALSE
str='abbcccddddeeeee' test = str.endswith('eee') print(test)輸出:TRUE
str='abbccc\tddddeeeee' test = str.expandtabs(16) print(test)輸出:abbccc ddddeeeee
str='abbccc\tddddeeeee' test = str.expandtabs() print(test)輸出:abbccc ddddeeeee
str='abbcccddddeeeee' test = str.find('dd') print(test)輸出:6
str='abbcccddddeeeee' test = str.find('f') print(test)輸出:-1
- 直接替換
str='{0},{1}' test = str.format('aa','bb') print(test) print(str)輸出:aa,bb
{0},{1}
- 指定替換
str='{Name},{age}' test = str.format(Name='Li',age='18') print(test) print(str)輸出:Li,18
{Name},{age}
- 使用列表替換
list = ['a','b',] list1 = ['c','d',] str='{0[0]},{1[1]}' test = str.format(list,list1) print(test)
- 格式限定
center='{:1^6}' test = center.format('abc') print(test) left='{:2>6}' test = left.format('abc') print(test) right='{:3<6}' test = right.format('abc') print(test)輸出:
222abc
abc333
- 精度與其他類型
str='{:.3f}' test = str.format(3.1415926) print(test) test = '{:b}'.format(20) print(test) test = '{:o}'.format(20) print(test) test = '{:d}'.format(20) print(test) test = '{:x}'.format(20) print(test)
10100 #二進制
24 #八進制
20 #十進制
14 #16進制
- 使用‘,’號作爲數字的分割
test = '{:,}'.format(2147483647) print(test)輸出:
str='adc' test = str.isalpha() print(test) str1='123adc' test = str1.isalpha() print(test)輸出:
False
str='adc' test = str.isdigit() print(test) str1='1234567890' test = str1.isdigit() print(test)輸出:
True
str='adc' test = str.islower() print(test) str1='ABcd' test = str1.islower() print(test)輸出:
False
str=' ' test = str.isspace() print(test) str1=' AB cd ' test = str1.isspace() print(test)輸出:
False
str='Hello Word' test = str.istitle() print(test) str1='Hello word!' test = str1.istitle() print(test)輸出:
False
str='HELLO WORD' test = str.isupper() print(test) str1='Hello WORD' test = str1.isupper() print(test)輸出:
False
str='*' test = str.join('hello') print(test)輸出:h*e*l*l*o
str='HELLO' test = str.ljust(10,'a') print(test)輸出:
str='HELLO' test = str.lower() print(test)輸出:
str='HELLO' test = str.lstrip('H') print(test)輸出:
str='hello word' test = str.partition('o') print(test)輸出:
str='hellowordosdsossdo' test = str.replace('o','TEST',2) print(test)輸出:
str='hello' test = str.rfind('o') print(test)輸出:
str='hello' test = str.rfind('o',0,3) print(test)輸出:
str='hello' test = str.rindex('x') print(test)輸出:
str='hello' test = str.rjust(8,'T') print(test)輸出:
str='hello' test = str.rpartition('l') print(test)輸出:
str='h-e-l-l-o' test = str.rsplit('-') print(test)輸出:
str='h-e-l-l-o' test = str.rsplit('-',2) print(test)輸出:
str='h-e-l-l-o' test = str.rstrip('o') print(test)輸出:
str='h-e-l-l-o' test = str.split('-') print(test)輸出:
str='h-e-l-l-o' test = str.split('-',3) print(test)輸出:
str='hello\nword' test = str.splitlines() print(test)輸出:
str='hello\nword' test = str.splitlines(True) print(test)
str='helloword' test = str.startswith('he') print(test)輸出:
str='helloword' test = str.startswith('hel',0,1) print(test)輸出:
str=' helloword ' test = str.strip() print(test)輸出:
str='----helloword----' test = str.strip('-') print(test)輸出:
str='helloword' test = str.swapcase() print(test) print(test.swapcase())輸出:
helloword
str='hello word' test = str.title() print(test)輸出:
str='hello word' test = str.upper() print(test)輸出:
str='hello' test = str.zfill(10) print(test)輸出: