美文网首页
2023-07-13Python:去除字符串首尾空格

2023-07-13Python:去除字符串首尾空格

作者: 饮者L | 来源:发表于2023-07-12 09:43 被阅读0次

要去除字符串首尾的空格,可以使用Python的strip()方法。这个方法会返回一个去除了首尾空格的新字符串。

以下是一个示例,演示了如何使用strip()方法去除字符串首尾的空格:

# 去除字符串首尾空格
# -!- coding: utf-8 -!-
string = "  Hello, World!  "
trimmed_string = string.strip()

print(trimmed_string)  # 输出 "Hello, World!"

在这个示例中,我们定义了一个字符串string,其中包含了首尾的空格。通过调用strip()方法,我们得到了一个去除了首尾空格的新字符串trimmed_string。

希望这个回答能帮助你去除字符串首尾的空格!如果你有任何其他问题,请随时问我

相关文章

网友评论

      本文标题:2023-07-13Python:去除字符串首尾空格

      本文链接:https://www.haomeiwen.com/subject/netpudtx.html