python中str.strip()怎么使用

655Z技术栈 Python编程 4小时前 4

str.strip()方法用于去除字符串首尾指定字符,默认去除空格。

使用方法如下:

str.strip([chars])

其中,chars为可选参数,用于指定需要去除的字符。

示例:

s = " hello world " print(s.strip()) # 输出:"hello world" s = " hello world " print(s.strip(" ")) # 输出:"hello world" s = "#hello world#" print(s.strip("#")) # 输出:"hello world"

请注意,str.strip()方法只能去除字符串首尾的指定字符,中间部分不会被去除。如果需要去除字符串中所有指定字符,可以使用str.replace()方法。

提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。

邮箱:yvsm@163.com 微信:yvsm316 QQ:316430983
关闭

用微信“扫一扫”