Python:去除字符串首尾空格
要去除字符串首尾的空格,可以使用Python的strip()方法。这个方法会返回一个去除了首尾空格的新字符串。
以下是一个示例,演示了如何使用strip()方法去除字符串首尾的空格:
# 去除字符串首尾空格string = " Hello, World! "trimmed_string = string.strip()print(trimmed_string) # 输出 "Hello, World!"
在这个示例中,我们定义了一个字符串string,其中包含了首尾的空格。通过调用strip()方法,我们得到了一个去除了首尾空格的新字符串trimmed_string。
希望这个回答能帮助你去除字符串首尾的空格!如果你有任何其他问题,请随时问我