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)输出: