1、 isalnum() :判断字符串所有的字符都是字母或者数字。返回true和false
In [1]: str1='jiangwei520'In [2]: str2='jiang wei'In [3]: str3='520'In [4]: str4='520 1314'In [5]: str1.isalnum()Out[5]: TrueIn [6]: str2.isalnum()Out[6]: FalseIn [7]: str3.isalnum()Out[7]: TrueIn [8]: str4.isalnum()Out[8]: False
2、 isalpha() :判断字符串所有的字符都是字母。返回true和false
In [11]: s1='j w'In [12]: s2='jw'In [13]: s1.isalpha()Out[13]: FalseIn [14]: s2.isalpha()Out[14]: True
3、 isdigit(): 判断字符串所有的字符都是数字。返回true和false
In [15]: n1='12 34'In [16]: n2='1234'In [17]: n3='1.1'In [18]: n1.isdigit()Out[18]: FalseIn [19]: n2.isdigit()Out[19]: TrueIn [20]: n3.isdigit()Out[20]: False
4、 islower() :判断所有的字符都是小写。
In [23]: s1='j w'In [24]: s2='jw'In [25]: s3='JW'In [26]: s1.islower()Out[26]: TrueIn [27]: s2.islower()Out[27]: TrueIn [28]: s3.islower()Out[28]: False
5、 isupper() :判断所有的字符都是大写。
In [29]: s1='J w'In [30]: s2="J W"In [31]: s3="JW"In [32]: s4='Jw'In [33]: s1.isupper()Out[33]: FalseIn [34]: s2.isupper()Out[34]: TrueIn [35]: s3.isupper()Out[35]: TrueIn [36]: s4.isupper()Out[36]: False
6、 istitle() :判断每个单词的首字母都是大写。
In [37]: s1='hello world'In [38]: s2='Hello World'In [39]: s3='Hello,world'In [40]: s4='HELLO WORLD'In [41]: s1.istitle()Out[41]: FalseIn [42]: s2.istitle()Out[42]: TrueIn [43]: s3.istitle()Out[43]: FalseIn [44]: s4.istitle()Out[44]: False
7、 lower() :转小写
In [47]: s4Out[47]: 'HELLO WORLD'In [48]: s4.lower()Out[48]: 'hello world'In [49]: s2Out[49]: 'Hello World'In [50]: s2.lower()Out[50]: 'hello world'
7、 upper() :转大写
In [54]: s1Out[54]: 'HEllo WOrld'In [55]: s3Out[55]: 'Hello,world'In [56]: s1.upper()Out[56]: 'HELLO WORLD'In [57]: s3.upper()Out[57]: 'HELLO,WORLD'
8、 strip([chars]) :去除
lstrip()和 rstrip() 类似In [61]: s1=' hello world !!! 'In [62]: s1.strip()Out[62]: 'hello world !!!'In [63]: s2='**** jw---love---you ****'In [64]: s2.strip('*')Out[64]: ' jw---love---you '#应该是去除两边的 In [107]: a='***111***' In [108]: a.lstrip('*') Out[108]: '111***' In [109]: a.rstrip('*') Out[109]: '***111' In [110]: a.strip('*') Out[110]: '111'
9、 replace(old ,new, [count]) :替换
In [72]: a='小喵和小九'In [73]: a.replace('喵','喵喵').replace('九','九九')Out[73]: '小喵喵和小九九'In [74]: b='jiangwei is a good good good boy'In [75]: b.replace('good','nice')Out[75]: 'jiangwei is a nice nice nice boy'In [76]: b.replace('good','nice',2)Out[76]: 'jiangwei is a nice nice good boy'In [77]: b.replace('good','nice',1)Out[77]: 'jiangwei is a nice good good boy'
新闻热点
疑难解答