python 字符串操作
1.list("hello") #=>['h', 'e', 'l', 'l', 'o']
2.tuple("hello") #=>('h', 'e', 'l', 'l', 'o')
3.for c in "hello":
print c
打印结果:
h
e
l
l
o
4.set("hello") #=>{l,e,o,h}
5."".join(set1 & set2) #=>new str
6.字符串对齐方式:
- # 左对齐,居中对齐,右对齐
- print('|', 'gzdk'.ljust(20), '|', 'gzdk'.center(20), '|', 'gzdk'.rjust(20))
- # 指定填充字符
- print('gzdk'.center(20, '*'))
输出:
| gzdk | gzdk | gzdk
********gzdk********
7.# 去除字符串两端的空格
print('**********************************************')
org_str = ' python '
print('|', org_str.lstrip(), '|', org_str.rstrip(), '|', org_str.strip(), '|')
8.# 去除字符串两端的指定字符
org_str = '***+++*+*+ python ++*+*+*+*+++'
print('|', org_str.strip('+*'), '|')
print('|', org_str.strip('+* '), '|')
输出:
**********************************************
| python | python | python |
| python |
| python |
9.
str="hello"
Str.lower() #小写
Str.upper() #大写
Str.swapcase() #大小写互换
Str.capitalize() #首字母大写
str.title() #只有首字母大写,其余为小写
S.startwith(prefix[,start[,end]])
#是否以prefix开头
S.endwith(suffix[,start[,end]])
#以suffix结尾
S.isalnum()
#是否全是字母和数字,并至少有一个字符
S.isalpha() #是否全是字母
S.isdigit() #是否全是数字
S.isspace() #是否全是空白字符
S.islower() #S中的字母是否全是小写
S.isupper() #S中的字母是否便是大写
S.istitle() #S是否是首字母大写的
参考链接:
http://blog.csdn.net/guzhou_diaoke/article/details/8253360
http://www.cnblogs.com/SunWentao/archive/2008/06/19/1225690.html
浙公网安备 33010602011771号