0
a quiz
Your job is to write a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number the number 1 should be appended to the new string. Examples: foo -> foo1 foobar23 -> foobar24 foo0042 -> foo0043 foo9 -> foo10 foo099 -> foo100 Attention: If the number has leading zeros the amount of digits should be considered.
4 Antworten
+ 4
def increment_string(str_):
num = 0
for i, s in enumerate(str_):
if s.isdigit():
num = str_[i:]
s = str_[:i]
break
if num == 0:
return str_ + '1'
else:
return s + str(int(num)+1).zfill(len(num)) #'abc'.zfill(4) = 0abc
print(increment_string('foo099'))
+ 2
Are you having some issue with that task? Liu zirui
0
def increment_string(str):
a =[]
for i in reversed(str):
while i.isdigit():
a.append(int(i))
break
return(a)
0
the uncompleted work