glls
glls

Reputation: 2403

inverse characters from a string using 'for i in string'

In python is there a way to inverse a string using a 'for i in string' but starting from the last char rather than the first? I would like to avoid using a counte rand I do not want to use INVERSE. Thanks for the feedback!

string = 'caramelosticosis'
new_string = ''
cont = 0
for i in string:
    cont+=1
    new_string += string[-cont]



print(new_string)

Upvotes: 0

Views: 98

Answers (1)

dmcc
dmcc

Reputation: 2529

Easiest way is to just use a slice:

text = 'caramelosticosis'
print(text[::-1])

(By the way, best not to use string as a variable name as it is also the name of a module)

Upvotes: 2

Related Questions