# 示例代码:使用 strip 函数去除字符串首尾的空白字符
# 定义一个包含首尾空格的字符串
text = " 去除首尾空格 "
# 使用 strip() 函数去除首尾的空格
stripped_text = text.strip()
# 输出结果
print(f"原始字符串: '{text}'")
print(f"去除空格后: '{stripped_text}'")
# 解释说明:
# strip() 函数用于移除字符串开头和结尾的指定字符(默认是空白字符,包括空格、制表符和换行符)。
# 如果需要移除其他特定字符,可以在 strip() 函数中传入这些字符。例如:strip('abc') 将会移除字符串首尾的 'a'、'b' 或 'c' 字符。
# 示例代码:使用 strip 函数去除字符串首尾的指定字符
# 定义一个包含首尾指定字符的字符串
text_with_chars = "###去除首尾指定字符###"
# 使用 strip() 函数去除首尾的 '#' 字符
stripped_text_with_chars = text_with_chars.strip('#')
# 输出结果
print(f"原始字符串: '{text_with_chars}'")
print(f"去除指定字符后: '{stripped_text_with_chars}'")
下一篇:python pdf合并
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站