0 points
Submitted by Cawarnold
almost 8 years

7/15 'str' object has no attribute 'append'

@criticizing monster

I am getting an error that i don’t get.

I’ve read this but still don’t really understand. http://stackoverflow.com/questions/4005796/attributeerror-str-object-has-no-attribute-append

def reverse(text):
    x = len(text)
    reve = ""
    print text[x-1]
    while x > 0:
        reve.append([x-1])  
        x = x - 1
    return reve    

print reverse("Python!") 

Traceback (most recent call last):
  File "<stdin>", line 21, in <module>
  File "<stdin>", line 16, in reverse
AttributeError: 'str' object has no attribute 'append'

Answer 52db9484631fe90752004b6b

1 vote

Permalink

My solution of 7/15:

 def reverse (text):
    rev = []
    tmp = list(text)
    for l in tmp:
        rev.insert(0,l)
    return ''.join(rev)
points
Submitted by Alexandr Stavitsky
almost 8 years

Answer 52e610bf80ff331a77001a12

1 vote

Permalink

Methods are essentially functions designed for use with their type (str, int, dictionary, etc). append() is a list method, so it can’t be used with the other types. It’s as simple as that.

And technically, strings can’t have a method like append() because they are immutable. When you do something like string += character you’re not actually modifying the existing string, you’re creating a whole new string. But since lists are mutable, they can truly be appended to.

points
Submitted by Asher Higgs
almost 8 years

Answer 52d72cee9c4e9d8d110003f8

0 votes

Permalink

def reverse(text): a = text #put her for more clarity when used in for loop string = [] #establishes a list to make use of its operators for i in a:
string.insert(0, i) #Each time you add a letter, it is place at position 0 return “”.join(string) #Removes the list formatting

points
Submitted by chronig
almost 8 years

2 comments

chronig almost 8 years

Strings do not allow you to append. To handle this simply, you can put each item into a list

mark keily almost 8 years

… you can append to a string… technical… just do “”” reve += Text.To.Append ‘“””