Python 字符串去空格函数:strip() 的用法

在 Python 中,可以使用 strip() 函数去除字符串左右两边的空格。

示例:

string = '  Hello World  '
stripped_string = string.strip()
print(stripped_string)  # 输出:Hello World

其他选项:

  • center() 函数可以将字符串居中,并用指定字符(默认为空格)填充至指定长度。

  • count() 函数可以统计字符串中某个子串出现的次数。

  • format() 函数可以将字符串中的占位符替换为指定的值,并返回替换后的字符串。

Python 字符串去空格函数:strip() 的用法

原文地址: https://www.cveoy.top/t/topic/f2zG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录