I'm a beginner and I'm trying to make a palindrome from a user input. I was thinking I could use .replace() but I got an error that 'str' object has no attribute 'append'. I thought strings were lists? So why don't list methods work on them?
-
9Nope, strings are not lists. – cs95 Jan 23 '18 at 16:40
-
3Strings are sequences, like lists. Read about `list()` in the docs. – Michael Butscher Jan 23 '18 at 16:41
-
strings are not lists. If you want to add text at the end of a string use `+` e.g. `str1+str2` – usernamenotfound Jan 23 '18 at 16:44
-
Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Jan 23 '18 at 16:55
5 Answers
Strings are not lists, they are sequences. They share some of the same behaviors as lists, but they are not themselves lists. Strings are immutable objects.
You can convert a string to a list by passing the string to list
:
data = list("some string")
You can then operate on data
, and convert it back to a string later with join
:
new_string = "".join(data)
If you simply want to add a character to a string you can use concatenation:
new_string = "abc" + "d"

- 370,779
- 53
- 539
- 685
Strings are a distinct class from Lists. As the error says, Strings don't have an append
method.
append
wouldn't make sense for Strings anyways, as they're immutable. They can't be modified in place like lists can.
If you want to "append" a Character, just use concatenation, and reassign the String:
x = "String" + "s"

- 43,494
- 9
- 68
- 117
To get things clear, str
and list
are two different data types.
One basic difference between str's and lists is that lists can any type of data i.e. integers, characters, dictionary or another list etc., while strings can only hold a set of characters(alphabets numbers or any special characters).
Also, lists are mutable and string are immutable!
To get to know what methods can be used with a string dir(str)
and dir(list)
would help!

- 3,766
- 1
- 13
- 23
Tl;dr: strings
!= lists
.
You can index individual characters in strings
, like you can index elements in lists
, but there is a difference in mutability.
If something is mutable, it can change. Lists
are mutable so you can change the contents of them (either adding, removing or changing elements).
See some examples that I am sure you are familiar with:
>>> l = [1, 2, 3]
>>> l[0] = 9
>>> l.append(8)
>>> l
[9, 2, 3, 8]
On the other hand, strings
are immutable so can not change. This means that there are no append
or equivalent methods. However, you can concatenate
two of them to form a new string, but this is a different operation in terms of memory (appending modifies the current space in memory, concatenation assigns a whole new space in memory to the new string).
So here are some examples with strings
:
>>> s = "abcdef"
>>> s += "ghi"
>>> s
'abcdefghi'
>>> s[0] = "z"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

- 20,101
- 7
- 33
- 54
You can use Python's type()
method to figure out what type the object is:
>>> type("String")
<class 'str'>
You can also turn the string into a list using the list method:
>>> type(list("String"))
<class 'list'>

- 1,365
- 7
- 34
- 62