Python字符串常用方法详解:判断、转换与处理

花开有时
花开有时
发布时间:2026-10-13 | 6人浏览过
原创

本文系统介绍Python中常用的字符串处理方法,涵盖isalnum、isalpha、isdigit等判断方法,lower、upper、strip等转换与清理方法,以及replace、split、join、format等高级操作。通过具体示例解析各方法的参数与返回值,帮助开发者高效处理文本数据。

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]: True
In [6]: str2.isalnum()
Out[6]: False
In [7]: str3.isalnum()
Out[7]: True
In [8]: str4.isalnum()
Out[8]: False

isalpha()方法用于判断字符串中的所有字符是否均为字母。若全部为字母则返回True,否则返回False。

In [11]: s1='j w'
In [12]: s2='jw'
In [13]: s1.isalpha()
Out[13]: False
In [14]: s2.isalpha()
Out[14]: True

isdigit()方法用于判断字符串中的所有字符是否均为数字。若全为数字则返回True,否则返回False。

In [15]: n1='12 34'
In [16]: n2='1234'
In [17]: n3='1.1'
In [18]: n1.isdigit()
Out[18]: False
In [19]: n2.isdigit()
Out[19]: True
In [20]: n3.isdigit()
Out[20]: False

islower()方法用于检查字符串中的所有字符是否均为小写字母。

In [23]: s1='j w'
In [24]: s2='jw'
In [25]: s3='JW'
In [26]: s1.islower()
Out[26]: True
In [27]: s2.islower()
Out[27]: True
In [28]: s3.islower()
Out[28]: False

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]: False
In [34]: s2.isupper()
Out[34]: True
In [35]: s3.isupper()
Out[35]: True
In [36]: s4.isupper()
Out[36]: False

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]: False
In [42]: s2.istitle()
Out[42]: True
In [43]: s3.istitle()
Out[43]: False
In [44]: s4.istitle()
Out[44]: False

lower()方法用于将字符串中的所有字符转换为小写。

In [47]: s4
Out[47]: 'HELLO WORLD'
In [48]: s4.lower()
Out[48]: 'hello world'
In [49]: s2
Out[49]: 'Hello World'
In [50]: s2.lower()
Out[50]: 'hello world'

upper()方法用于将字符串中的所有字符转换为大写。

In [54]: s1
Out[54]: 'HEllo WOrld'
In [55]: s3
Out[55]: 'Hello,world'
In [56]: s1.upper()
Out[56]: 'HELLO WORLD'
In [57]: s3.upper()
Out[57]: 'HELLO,WORLD'

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'

replace(old, new, [count])方法用于将字符串中的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'

split()方法用于将字符串按指定分隔符切割,返回一个列表。

In [92]: path1
Out[92]: 'a/b/c/d'
In [93]: path1.split('/')
Out[93]: ['a', 'b', 'c', 'd']
In [88]: path='/home/centos/python3.6'
In [89]: path
Out[89]: '/home/centos/python3.6'
In [90]: path.split('/')
Out[90]: ['', 'home', 'centos', 'python3.6']

startswith()方法用于判断字符串是否以指定的子串开头,返回True或False。

endswith():类似
In [94]: a='helloworld'
In [95]: b='hello world'
In [96]: a.startswith('hello')
Out[96]: True
In [97]: b.startswith('hello')
Out[97]: True
In [98]: b.startswith('he')
Out[98]: True

format()方法用于进行字符串格式化输出,支持通过关键字或位置参数填充占位符。

In [111]: print('{name}---->{age}'.format(name='xjm',age=21))
xjm---->21
In [112]:

title()方法用于将字符串中每个单词的首字母转换为大写。

In [112]: s='hello world python '
In [113]: s.title()
Out[113]: 'Hello World Python '
#与capitalize不同。就第一个单词的首字母大写
 In [128]: a='hello world python'
In [129]: a.capitalize()
Out[129]: 'Hello world python'

join()方法用于将序列中的元素以指定的字符串作为连接符合并成一个新字符串。

In [117]: a='.'
In [118]: a.join('jwlove')
Out[118]: 'j.w.l.o.v.e'
 In [124]: a='/'
In [125]: b=('user','local') 
In [127]: a.join(b)
Out[127]: 'user/local'

center(width, [char])方法用于将字符串居中显示,指定总宽度width,可选参数char用于填充两侧空白。

In [137]: a='Linux'
In [138]: a.center(25,'*')
Out[138]: '**********Linux**********'
In [140]: a.center(25)
Out[140]: '     Linux    '
#ljust和rjust类似
 In [142]: a.ljust(10,'-')
 Out[142]: 'Linux-----'
 In [143]: a.rjust(10,'-')
 Out[143]: '-----Linux'

splitlines()方法根据换行符(\r、\n、\r\n)将字符串切割,返回一个列表。

In [151]: a='如果\n没有\r如果'
In [154]: print(a)
如果
如果
In [157]: a.splitlines()
Out[157]: ['如果', '没有', '如果']

format_map()方法用于使用映射对象(如字典)进行字符串格式化输出。

In [158]: a='hello world {course}'
In [160]: course1='python'
In [161]: course2='java'
In [178]: a.format(course=course1)
Out[178]: 'hello world java'
In [179]: a.format(course=course2)
Out[179]: 'hello world python'
In [181]: a.format_map(vars())
Out[181]: 'hello world python'

总结

以上介绍了Python字符串的常用处理方法,希望能为读者提供实用参考。如有任何疑问,欢迎留言交流,小编将及时回复。感谢大家对本站的支持!

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表