基本字符串操作
所有标准的序列操作:
索引、分片、乘法、成员资格、长度、最大小值。
但是,字符串是不可变的,所以,分片赋值不合法。
>>> study = 'come on baby go'
>>> stydy[1:4] = 'no!'
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
stydy[1:4] = 'no!'
NameError: name 'stydy' is not defined
如果需要转换的元组作为转换表的一部分存在,那么必须将它用圆括号括起来,避免出错
>>> '%s plus %s equals %s' % (1,2,3)
'1 plus 2 equals 3'
>>> '%s plus %s equals %s' % 1,2,3
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
'%s plus %s equals %s' % 1,2,3
TypeError: not enough arguments for format string
(1)%字符
说明转换说明符的开始
(2)转换标志(可选)
-表示左对齐;
+表示在转换值之前要加上正负号;
“”(空白字符)表示正数之前保留空格;
0表示转换值若位数不够则用0填充。
(3)最小字段宽度(可选)
转换后的字符串至少应该具有该值指定的宽度。
如果是*,则宽度会从值元组中读出。
(4)点(.)后跟精度值(可选)
若实数,则是小数点后的位数
若字符串,则是最大字段宽度
如果是*,则宽度会从值元组中读出
(5)转换类型
d,i 带符号十进制整数
o 不带符号八进制
u 不带符号十进制
x 不带符号十六进制小写
X 不带符号十六进制大写
e 科学计数法表示的浮点数小写
E 科学计数法表示的浮点数大写
f,F 十进制浮点数
g 如果指数大于-4或者小于精度值则和e相同,其他情况与f相同
G 如果指数大于-4或者小于精度值则和E相同,其他情况与F相同
C 单字符(接受整数或者单字符字符串)
r 字符串(使用repr转换任意Python对象)
s 字符串(使用str转换任意Python对象)
字符串
>>> 'Can I learn python ? %s' % 'of cause'
'Can I learn python ? of cause'
整数
>>> 'Book price: %d' % 69
'Book price: 69'
>>> 'Book price: %i' % 69
'Book price: 69'
负数
>>> 'Book price: %i' % -69
'Book price: -69'
精度
>>> 'Book price: %f' % 69.00
'Book price: 69.000000'
>>> 'Book price: %.2f' % 69.00
'Book price: 69.00'
>>> '%.5s' % 'Come on baby go'
'Come '
>>> from math import pi
补充0
>>> '%010.2f' % pi
'0000003.14'
>>> '%010.5f' % pi
'0003.14159'
对齐
>>> '%-10.5f' % pi
'3.14159 '
>>> '%10.5f' % pi
' 3.14159'
字符串方法
find()
返回子串的最左端索引,找不到返回-1
>>> title = 'study the python in a month'
>>> title.find('python')
10
>>> title.find('sleep')
-1
join()
split()的逆方法
用来在队列中添加元素
>>> seq = [1,2,3]
>>> sign = '+'
>>> sign.join(seq)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
sign.join(seq)
TypeError: sequence item 0: expected string, int found
操作的是字符串才可以
>>> seq = ['1','2','3']
>>> sign = '+'
>>> sign.join(seq)
'1+2+3'
lower(),upper()
字符串的大小写转换
>>> 'HELLO WORLD'.lower()
'hello world'
>>> 'hello'.upper()
'HELLO'
标题转换
所有单词开头大写
>>> "that's all, folks".title() #这里用"",因为里面有',不冲突
"That'S All, Folks"
这个方法准确些
>>> import string
>>> string.capwords("that's all, folks")
"That's All, Folks"
replace()
返回某字符串的所有匹配项被替换后得到的字符串
>>> 'This is a test'.replace('is', 'eez')
'Theez eez a test'
split()
用来将字符串分割成序列
>>> '1+2+3+4_5'.split('+')
['1', '2', '3', '4_5']
strip()
返回去除两侧(不包括内部)空格的字符串:
>>> ' com on '.strip()
'com on'
也可以指定去除的元素
>>> '****** come on* **'.strip('*')
' come on* ' #因为先遇到' ',所以on后的*没有去除
translate()
类似replace,但只处理单个字符。
优势可以进行多个替换,有时效率更高。
使用translate之前,需要先完成一张转换表。
转换表中是以某字符替换某字符的对应关系。
使用string模块里的maketrans函数就可以了
>>> from string import maketrans
>>> table = maketrans('cs', 'kz')
>>> 'this is an incredible test'.translate(table)
'thiz iz an inkredible tezt'
第二个参数,用来指定需要删除的字符
>>> 'this is an incredible test'.translate(table, ' ')
'thizizaninkredibletezt'
小结:
1.字符串格式化
2.字符串方法
所有标准的序列操作:
索引、分片、乘法、成员资格、长度、最大小值。
但是,字符串是不可变的,所以,分片赋值不合法。
>>> study = 'come on baby go'
>>> stydy[1:4] = 'no!'
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
stydy[1:4] = 'no!'
NameError: name 'stydy' is not defined
如果需要转换的元组作为转换表的一部分存在,那么必须将它用圆括号括起来,避免出错
>>> '%s plus %s equals %s' % (1,2,3)
'1 plus 2 equals 3'
>>> '%s plus %s equals %s' % 1,2,3
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
'%s plus %s equals %s' % 1,2,3
TypeError: not enough arguments for format string
(1)%字符
说明转换说明符的开始
(2)转换标志(可选)
-表示左对齐;
+表示在转换值之前要加上正负号;
“”(空白字符)表示正数之前保留空格;
0表示转换值若位数不够则用0填充。
(3)最小字段宽度(可选)
转换后的字符串至少应该具有该值指定的宽度。
如果是*,则宽度会从值元组中读出。
(4)点(.)后跟精度值(可选)
若实数,则是小数点后的位数
若字符串,则是最大字段宽度
如果是*,则宽度会从值元组中读出
(5)转换类型
d,i 带符号十进制整数
o 不带符号八进制
u 不带符号十进制
x 不带符号十六进制小写
X 不带符号十六进制大写
e 科学计数法表示的浮点数小写
E 科学计数法表示的浮点数大写
f,F 十进制浮点数
g 如果指数大于-4或者小于精度值则和e相同,其他情况与f相同
G 如果指数大于-4或者小于精度值则和E相同,其他情况与F相同
C 单字符(接受整数或者单字符字符串)
r 字符串(使用repr转换任意Python对象)
s 字符串(使用str转换任意Python对象)
字符串
>>> 'Can I learn python ? %s' % 'of cause'
'Can I learn python ? of cause'
整数
>>> 'Book price: %d' % 69
'Book price: 69'
>>> 'Book price: %i' % 69
'Book price: 69'
负数
>>> 'Book price: %i' % -69
'Book price: -69'
精度
>>> 'Book price: %f' % 69.00
'Book price: 69.000000'
>>> 'Book price: %.2f' % 69.00
'Book price: 69.00'
>>> '%.5s' % 'Come on baby go'
'Come '
>>> from math import pi
补充0
>>> '%010.2f' % pi
'0000003.14'
>>> '%010.5f' % pi
'0003.14159'
对齐
>>> '%-10.5f' % pi
'3.14159 '
>>> '%10.5f' % pi
' 3.14159'
字符串方法
find()
返回子串的最左端索引,找不到返回-1
>>> title = 'study the python in a month'
>>> title.find('python')
10
>>> title.find('sleep')
-1
join()
split()的逆方法
用来在队列中添加元素
>>> seq = [1,2,3]
>>> sign = '+'
>>> sign.join(seq)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
sign.join(seq)
TypeError: sequence item 0: expected string, int found
操作的是字符串才可以
>>> seq = ['1','2','3']
>>> sign = '+'
>>> sign.join(seq)
'1+2+3'
lower(),upper()
字符串的大小写转换
>>> 'HELLO WORLD'.lower()
'hello world'
>>> 'hello'.upper()
'HELLO'
标题转换
所有单词开头大写
>>> "that's all, folks".title() #这里用"",因为里面有',不冲突
"That'S All, Folks"
这个方法准确些
>>> import string
>>> string.capwords("that's all, folks")
"That's All, Folks"
replace()
返回某字符串的所有匹配项被替换后得到的字符串
>>> 'This is a test'.replace('is', 'eez')
'Theez eez a test'
split()
用来将字符串分割成序列
>>> '1+2+3+4_5'.split('+')
['1', '2', '3', '4_5']
strip()
返回去除两侧(不包括内部)空格的字符串:
>>> ' com on '.strip()
'com on'
也可以指定去除的元素
>>> '****** come on* **'.strip('*')
' come on* ' #因为先遇到' ',所以on后的*没有去除
translate()
类似replace,但只处理单个字符。
优势可以进行多个替换,有时效率更高。
使用translate之前,需要先完成一张转换表。
转换表中是以某字符替换某字符的对应关系。
使用string模块里的maketrans函数就可以了
>>> from string import maketrans
>>> table = maketrans('cs', 'kz')
>>> 'this is an incredible test'.translate(table)
'thiz iz an inkredible tezt'
第二个参数,用来指定需要删除的字符
>>> 'this is an incredible test'.translate(table, ' ')
'thizizaninkredibletezt'
小结:
1.字符串格式化
2.字符串方法