1、strip()函数用于删除指定字符串头尾信息(默认为空格)
不加参数:
s = ' this is a example ' print(s) print(s.strip())
"D:\Program Files (x86)\python.exe" D:/python/file.py this is a example this is a example Process finished with exit code 0
加参数:
s = ' 0000this is a example 00000' print(s) print(s.strip('0'))
"D:\Program Files (x86)\python.exe" D:/python/file.py
0000this is a example 00000
0000this is a example
Process finished with exit code 0
s = '0000 this is a example 00000' print(s) print(s.strip('0'))"D:\Program Files (x86)\python.exe" D:/python/file.py
0000 this is a example 00000
this is a example
Process finished with exit code 0
本文介绍了Python编程中strip()函数的使用,该函数主要用于移除字符串开头和结尾的特定字符,默认为空格。通过实例展示了如何不加参数及设置参数进行操作。

290

被折叠的 条评论
为什么被折叠?



