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.
我有一个字符串,稍后将使用int(). 它是三位数字,其中 0 到 3 个可能是 0。我将如何从字符串的左侧去除 0?
int()
现在我正在使用string.lstrip('0'),但这会去除所有的 0 并使字符串为空,从而导致错误。
string.lstrip('0')
你可以这样做:
s = str(int(s))
另一种选择是:
s = s.lstrip('0') or '0'
你想要str.lstrip()那个。但也许你应该只将基数传递给int().
str.lstrip()
怎么样string[:-1].lstrip('0')?:D
string[:-1].lstrip('0')