我是 python 的新手。我想修剪一个字符串仅当它的最后 6 位数字为零时。 我检查了 strip() 方法,但没有看到指示要从右侧修剪的 n 个 个字符的方法。
示例字符串:
20121124000000
结果字符串应该是:
20121124
请您参考如下方法:
s = s[:-6] if s.endswith('000000') else s
或者定义一个函数:
def rstrip_chars(s, chars):
return s[:-len(chars)] if s.endswith(chars) else s
s = rstrip_chars(s, '000000')
