# 示例代码:去掉字符串中的空格
# 方法1:去掉字符串两端的空格
s = " hello world "
s.strip() # 结果为 "hello world"
# 解释:strip() 方法用于移除字符串头尾指定的字符(默认为空格)
# 方法2:去掉字符串中所有的空格
s = " hello world "
s.replace(" ", "") # 结果为 "helloworld"
# 解释:replace() 方法可以把字符串中的所有指定字符替换为其他字符,这里将空格替换为空字符串
# 方法3:去掉字符串中的左边或右边的空格
s = " hello world "
s.lstrip() # 结果为 "hello world "
# 解释:lstrip() 方法用于移除字符串左侧的指定字符(默认为空格)
s.rstrip() # 结果为 " hello world"
# 解释:rstrip() 方法用于移除字符串右侧的指定字符(默认为空格)
上一篇:python 查看类型
下一篇:bin函数的用法python
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站