1
I need a recursive function that returns the string it takes as the inverted form parameter,arrived in this role:
def stringinvert(palavra):
if palavra < 10:
print(palavra)
return stringinvert(palavra[::-1])
print(stringinvert('teste'))
But it creates an error that in this case is this one:
if palavra < 10:
TypeError: '<' not supported between instances of 'str' and 'int'
Well, that was predictable for me. The question is: the recursive step is the string having only one letter, I return only this letter, the problem is how I will create this condition in Python ?
Always learning, never knowing
[::-1]
. By the way, can you explain the difference between[:]
and[::]
? :)– lazyFox
@lazyFox, look at it this way,
lista[index_start:index_end:step]
, and where the step can be negative, generating the inverted list in case it is-1
. Here are more complete explanations: https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation– Miguel
Thank you @Miguel :) very useful this link.
– lazyFox