字符串是Python中最常用的数量类型,我们可以使用单引号或双引号创建字符串;
转义字符 | 描述 |
\\ | 反斜杠符号 |
\‘ | 单引号 |
\" | 双引号 |
\a | 响铃 |
\b | 退格(BackSpace) |
\e | 转义 |
\000 | 空 |
\n | 换行 |
\v | 纵向制表符 |
\t | 横向制表符 |
\r | 回车 |
\f | 换页 |
\oyy | 八进制数,yy代表的字符。例如:\o12代表换行 |
\xyy | 十六进制数,yy代表的字符。例如:\x0a代表换行 |
\other | 其它的字符以普通格式输 |
访问字符串中的值,也可以使用方括号来截取字符串中的值;
a ="This is String!"
print(a,a[1])
ThisisString! h
更新字符串中的值;
a ="Hello World!"
print(a[:6]+‘Saviorsyang‘)
HelloSaviorsyang
字符串运算符之连接;
a =‘Hello ‘
b =‘World!‘
print(a+b)
HelloWorld!
字符串运算符之重复;
a =‘Hello ‘
print(a*4)
HelloHelloHelloHello
字符串运算符之索引;
a =‘Hello World‘
print(a[0])
H
字符串运算符之截取;
a =‘Hello World‘
print(a[0:5])
Hello
print(a[:-1])
HelloWorl
print(a[:5])
Hello
print(a[:-5])
Hello
字符串运算符之in;
a =‘Hello World‘
print(‘l‘in a)
True
print(‘x‘in a)
False
字符串运算符之not in;
a =‘Hello World‘
print(‘l‘notin a)
False
print(‘x‘notin a)
True
字符串格式化之%s,格式化字符串;
print("My Name is %s!!"%("Saviorsyang"))
MyNameisSaviorsyang!!
字符串格式化之%d,格式化整数;
print("My Age %s!!"%(‘25‘))
MyAge25!!
字符串格式化之%f,格式化浮点数,%2.f,保留小数点后2位;
print("harry weight %f kg, tom weight ‘%.2f‘kg。。"%(56.445,45.33))
harry weight 56.445000 kg, tom weight ‘45.33‘kg。。
capitalizecapitalize:将第一个字符串大写;
a ="hello world"
print(a.capitalize())
Hello world
casefold:将所有字符串小写;
a ="HELLO WORLD"
print(a.casefold())
hello world
a ="HELLO WORLD"
print(a.center(40,‘*‘))
**************HELLO WORLD***************
print(a.center(40))
HELLO WORLD
count:统计某个字符串出现的次数;
a ="HELLO WORLD"
print(a.count(‘L‘))
3
encode:
a ="HELLO WORLD"
print(a.encode(‘gbk‘))#以gbk编码对unicode对像进行编码
b‘HELLO WORLD‘
print(a.encode(‘utf-8‘))#以utf-8编码对unicode对像进行编码
b‘HELLO WORLD‘
endswith:判断字符串是否以指定的后缀结尾,如果以指定的后缀结尾返回True,否则返回False;
a ="HELLO WORLD"
print(a.endswith(‘LD‘,0,15))
True
print(a.endswith(‘LD‘))
True
print(a.endswith(‘LD‘,-1))
False
expandtabs:把字符串中的tab符号转为空格,默认8个空格;
name =‘h\tarry‘
print(name)
h arry
print(name.expandtabs())
h arry
print(name.expandtabs(16))
h arry
find:打印出在string中找到第一个匹配字符串值的索引位置;
a ="HELLO WORLD"
print(a.find(‘L‘))
2
print(a.find(‘L‘,1,5))
2
print(a.find(‘L‘,2))
2
format:
a ="Harry {0} as {1}!"
print(a.format(‘BT‘,‘SB‘))
Harry BT as SB!
a ="Harry {id1} as {id2}!"
print(a.format(id1=‘BT‘,id2=‘SB‘))
Harry BT as SB!
index:检测字符串中是否包含字符串str,如果指定开始和结束范围,则检查是否包含在指定范围内,在则显示索引位置,否则报一个异常;
a ="harry"
print(a.index(‘y‘))
4
print(a.index(‘y‘,4))
4
isalnum:如果string至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False;
a ="20170116Student"
print(a.isalnum())
True
a ="20170116 Student"
print(a.isalnum())
False
isalpha:如果string至少有一个字符并且所有字符都是字母则返回True,否则返回False;
a ="20170116Student"
print(a.isalpha())
False
a ="Student"
print(a.isalpha())
True
isdecimal:如果string至少有一个字符并且所有字符都是十进制字符则返回True,否则返回False;
a ="20170116Student"
print(a.isdecimal())
False
a ="20170116"
print(a.isdecimal())
True
isdigit:如果string至少有一个字符并且所有字符都是数字则返回True,否则返回False;
a ="20170116Student"
print(a.isdigit())
False
a ="20170116"
print(a.isdigit())
True
islower:判断所有字符是不是小写,是则True,否则False;;
a ="Student"
print(a.islower())
False
a ="student"
print(a.islower())
True
isnumeric:如果string中只包含数字字符则返回True,否则返回False;
a ="20170116Student"
print(a.isnumeric())
False
a ="20170116"
print(a.isnumeric())
True
isspace:如果string中只包含空格则返回True,否则返回False;
a ="20170116 Student"
print(a.isspace())
False
a =" "
print(a.isspace())
True
isupper:判断所有字符是不是大写,是则True,否则False;
a ="Student"
print(a.isupper())
False
a ="STUDENT"
print(a.isupper())
True
join:将序列中的元素以指定的字符连接生成一个新的字符串;
a ="_"
b =(‘a‘,‘b‘,‘c‘)
print(a.join(b))
a_b_c
ljust:返回一个原字符串左对齐,并使用*填充至指定长度的新字符串,默认使用空格填充;
a ="this is test ljust..."
print(a.ljust(50,‘*‘))
this is test ljust...*****************************
lower:转换所有大写字母为小写;
a ="STUDENT"
print(a.lower())
student
lstrip:去除左边的空格;
a =" Student Python"
print(a)
StudentPython
print(a.lstrip())
StudentPython
maketrans:创建字符串映射的转换表,接受两个参数的最简单调用方式,第一参数是字符串,表示需要转换的字符,第二个参数也是字符串,表示转换的目标,配合translate使用;
translate:字符串的一一映射,每个字符只要出现都会被替换为对应的字符;
a ="Hello Wolrd!"
b = a.maketrans(‘l‘,‘L‘)
a.translate(b)
‘HeLLo WoLrd!‘
a ="Hello Wolrd!"
b = a.maketrans(‘eordl‘,‘EORDL‘)
a.translate(b)
‘HELLO WOLRD!‘
partition:根据指定的分隔符将字符串进行分割,如果字符串包含指定的分隔符,则返回一个三元的元组,第一个为分隔符左边的字串,第二个为分隔符本身,第三个为分隔符右边的字串;
a ="http://www.baidu.com"
print(a.partition(‘://‘))
(‘http‘,‘://‘,‘www.baidu.com‘)
replace:字符串替换,字符串完整出现后被整体替换,replace的两个字符串参数长度可以不同,如果指定替换次数,则不超过替换次数;
a ="Hello Wolrd!"
print(a.replace(‘l‘,"L"))
HeLLoWoLrd!
print(a.replace(‘l‘,"L",2))
HeLLoWolrd!
rfind:从右边开始查找,如果没有匹配的值则返回-1;
a ="Hello Wolrd!"
print(a.rfind(‘l‘))
8
print(a.rfind(‘l‘,0,3))
2
print(a.rfind(‘l‘,0,1))
-1
rindex:从右边开始查找str在字符串中出现的位置,可以指定查找的区域,如果没有匹配则报错误;
a ="Hello Wolrd!"
print(a.rindex(‘l‘))
8
print(a.rindex(‘l‘,0,3))
2
print(a.rindex(‘l‘,0,1))
Traceback(most recent call last):
File"<input>", line 1,in<module>
ValueError: substring not found
rjust:返回一个原字符串右对齐,并使用*填充至指定长度的新字符串,默认使用空格填充;
a ="Hello Wolrd!"
print(a.rjust(20,‘*‘))
********HelloWolrd!
print(a.rjust(20))
HelloWolrd!
rpartition:根据指定的分隔符将字符串从右边进行分割,如果字符串包含指定的分隔符,则返回一个三元的元组,第一个为分隔符左边的字串,第二个为分隔符本身,第三个为分隔符右边的字串;
a ="www.qq.com"
print(a.rpartition(‘.‘))
(‘www.qq‘,‘.‘,‘com‘)
rsplit:通过指定的分隔符对字符串从右边开始进行切片,默认为所有的空字符,包括空格、换行、制表符等;
a ="Hello Wolrd !!!"
print(a.rsplit())
[‘Hello‘,‘Wolrd‘,‘!!!‘]
print(a.rsplit(‘ ‘,1))
[‘Hello Wolrd‘,‘!!!‘]
rstrip:去除右边的空格;
a ="Hello Wolrd!!! "
print(a.rstrip())
HelloWolrd!!!
split:通过指定的分隔符对字符串进行切片,默认为所有的空字符,包括空格、换行、制表符等;
a ="Hello Wolrd !!!"
print(a.split())
[‘Hello‘,‘Wolrd‘,‘!!!‘]
print(a.split(‘ ‘,1))
[‘Hello‘,‘Wolrd !!!‘]
splitlines:按行分割字符串,返回值也是一个列表。默认splitelines参数keepend为False,不保留每行结尾的\n,反之则保留;
a =‘a b\n \n de fg \rk1 \r\n‘
print(a)
a b
de fg
k1
print(a.splitlines())
[‘a b‘,‘ ‘,‘ de fg ‘,‘k1 ‘]
print(a.splitlines(True))
[‘a b\n‘,‘ \n‘,‘ de fg \r‘,‘k1 \r\n‘]
startswith:判断字符串是否以指定的前缀结尾,如果以指定的前缀结尾返回True,否则返回False;
a="Hello world!"
print(a.startswith(‘H‘))
True
print(a.startswith(‘j‘))
False
strip:去除左右两边的空格;
a=" Hello world! "
print(a.strip())
Hello world!
swapcase:翻转大小写字母;
a ="Hello World!"
print(a.swapcase())
hELLO wORLD!
title:所有单子的首字母大写;
a ="hello world"
print(a.title())
HelloWorld
upper:将所有小写字母转换为大写字母;
a ="hello world"
print(a.upper())
HELLO WORLD
zfill:指定长度的字符串,原字符串靠右,填充0;
a ="Hello World!"
print(a.zfill(15))
000HelloWorld!
print(a.zfill(20))
00000000HelloWorld!
原文:http://www.cnblogs.com/Saviorsyang/p/6295666.html