7、扫描字符串
>>> ss = '1sdfas23sdsdSD'
>>> for i in ss :
... print i,
...
1 s d f a s 2 3 s d s d S D
8、字符串翻转
>>> s = 'this is aa'
>>> s1 = s[::-1]
>>> s1
'aa si siht'
9、截取字符串
字符串索引与切片参看博文”Python数据结构之序列“
10、字符串在输出时的对齐
10.1 str.ljust()
函数原型:
str.ljust(width, [fillchar])
输出width长度个字符,str左对齐,不足部分用fillchar填充,默认为空格。
#使用*填充
>>> s1
'sddss'
>>> s1.ljust(10,'*')
'sddss*****'
#使用默认空格填充
>>> s1.ljust(10)
'sddss '
10.2 str.rjust()
函数原型:
str.rjust(width, [fillchar] )
输出width长度个字符,str右对齐,不足部分用fillchar填充,默认为空格。
>>> s1.rjust(10,'*')
'*****sddss'
>>> s1.rjust(10)
' sddss'
10.3 str.center()
函数原型:
str.center( width, [fillchar] )
输出width长度个字符,str中间对齐,不足部分用fillchar填充,默认为空格。
>>> s1.center(11,'*')
'***sddss***'
>>> s1.center(11)
' sddss '
10.4 str.zfill()
函数原型:
str.zfill( width)
把str变成width个长,并右对齐,不足部分用0补足。
>>> s1.zfill(10)
'00000sddss'