# 示例代码:使用 strip() 方法去除字符串首尾的空白字符
# 定义一个包含空白字符的字符串
text = " 去除首尾空格 "
# 使用 strip() 方法去除首尾的空白字符
stripped_text = text.strip()
# 输出结果
print(f"原始字符串: '{text}'")
print(f"去除空白字符后的字符串: '{stripped_text}'")
# 解释说明:
# strip() 方法用于移除字符串开头和结尾的指定字符(默认是空白字符,包括空格、制表符、换行符等)。
# 如果不传入参数,则默认去除空白字符;如果传入特定字符,则会去除这些字符。
# 示例代码:使用 strip() 方法去除指定字符
# 定义一个包含指定字符的字符串
text_with_chars = "...hello world..."
# 使用 strip() 方法去除指定字符 '.'
stripped_text_with_chars = text_with_chars.strip('.')
# 输出结果
print(f"原始字符串: '{text_with_chars}'")
print(f"去除指定字符后的字符串: '{stripped_text_with_chars}'")
上一篇:python路径
下一篇:python &&
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站