7. reverse


#1



TypeError: 'str' object does not support item assignment


Can I do this exercise with the method that I have begun? Can you explain me if there's some difference between using the first version and the second with the method for with strings?


#First version
def reverse(text):
    last_character=len(text)-1
    for letter in range(len(text)-1):
        text[letter]=text[last_character]
        last_character-=1
    return text 

#Second version
def reverse(text):
    last_character=len(text)-1
    for letter in text:
        text[letter]=text[last_character]
        last_character-=1
    return text


#2

Strings do not support modifying their content. Do something else that doesn't involve modifying them, or use a data structure that does support doing so.

If you do not need to know indexes, then creating a list of numbers to iterate through is redundant - iterate through the string instead


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.