可以使用Python的strip()方法去掉字符串两端的指定字符,默认情况下是去掉空格。因此,可以将“,”作为strip()方法的参数,去掉字符串两端的“,”字符。

示例代码:

s = ",,hello,world,,"
s = s.strip(",")
print(s)

输出结果:

hello,world
Python字符串去掉两头的号

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

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