The function is below. I'd like to use a while loop for this and no methods, as I want to get more practice using the while loop and if-statements.
def remove_leading_zeros(s):
'''(str) -> str
Return s, but without any extra leading zeros
e.g. given "007", return "7"
Precondition: Each character of s is a number.
'''
i = 0
r = ''
while i < len(s):
if s[i] != '0':
r = r + s[i]
i += 1
return r
So, when I type remove_leading_zeros('0001950'), I'd like the output to be '1950'.
However, with the function above, I seem to be omitting all the zeros. What should I modify to create a function that only omits the leading zeros?
I also tried r = r + s[i:] , so that as soon as a non zero is encountered, it would return everything afterwards, but I'm not sure how to end the loop at just that.
Resolved. Thanks to all that helped!