Why doesn't .append() method work on strings, don't they behave like lists?

谁说我不能喝 提交于 2020-04-10 07:27:12

问题


Why does this statement produce an error even though a string is actually a list of character constants?

string_name = ""  
string_name.append("hello word")  

The reason I expect this to work is because when we use for-loop, we are allowed to use this statement:

for i in string_name:  
    ...

I think string_name is considered as a list here(?)


回答1:


That's what they teach you in algorithms and data structures class, who deal with algorithmic languages (unreal) rather than real programming languages, in Python, a string is a string, and a list is a list, they're different objects, you can "append" to a string using what is called string concatenation (which is basically an addition operation on strings):

string_name = "hello"  
string_name = string_name + " world"
print(string_name) # => "hello world"

Or a shorthand concatenation:

string_name = "hello"
string_name += " world"
print(string_name) # => "hello world"

Lists and strings belong to this type called iterable. iterables are as they're name suggests, iterables, meaning you can iterate through them with the key word in, but that doesn't mean they're the same type of objects:

for i in '123': # valid, using string
for i in [1, 2, 3]: # valid, using list
for i in (1, 2, 3): # valid, using tuple
for i in 1, 2, 3: # valid, using an implicit-tuple
# all valid, all different types

I strongly recommend that you read the Python Documentation and/or take the Python's Tutorial.

From Docs Glossary:

iterable
An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator. More about iterables.




回答2:


Error is given when u try to append strings.So better first take list and then convert list to string.Code :

               n='qwerty'
               for i in range(1):
                   temp=[]
                   temp.append(n[-3:])
                   temp.append('t')
                   newtemp=' '
                   newtemp=temp[i]+temp[i+1]
                   print(newtemp)

Output:rtyt



来源:https://stackoverflow.com/questions/46141097/why-doesnt-append-method-work-on-strings-dont-they-behave-like-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!