# 示例代码:去除字符串中的空格
# 使用 strip() 方法去除字符串两端的空格
s = " hello world "
s_strip = s.strip()
print(f"使用 strip() 方法: '{s_strip}'") # 输出: 'hello world'
# 使用 lstrip() 方法去除字符串左边的空格
s_lstrip = s.lstrip()
print(f"使用 lstrip() 方法: '{s_lstrip}'") # 输出: 'hello world '
# 使用 rstrip() 方法去除字符串右边的空格
s_rstrip = s.rstrip()
print(f"使用 rstrip() 方法: '{s_rstrip}'") # 输出: ' hello world'
# 使用 replace() 方法去除字符串中所有的空格
s_replace = s.replace(" ", "")
print(f"使用 replace() 方法: '{s_replace}'") # 输出: 'helloworld'
strip():去除字符串两端的空格(包括制表符、换行符等空白字符)。lstrip():仅去除字符串左边的空格。rstrip():仅去除字符串右边的空格。replace(" ", ""):将字符串中的所有空格替换为空字符串,从而达到去除所有空格的效果。上一篇:python range(10)
下一篇:python回车符号
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站