Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是 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')