Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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(?)

like image 788
Piyush Kumar Avatar asked Sep 10 '17 13:09

Piyush Kumar


People also ask

Does append work with strings?

You can use the '+' operator to append two strings to create a new string. There are various ways such as using join, format, string IO, and appending the strings with space.

Why append method is not available in string class?

you are using 'outstring' variable of String class, and String class doesn't have append method. You should use StringBuilder or StringBuffer class.

Can you append a string into a list?

If you simply want to concatenate a string 'n' times, you can do it easily using s = 'Hi' * 10 . Another way to perform string append operation is by creating a list and appending strings to the list. Then use string join() function to merge them together to get the result string.

Does append work on lists?

Lists are sequences that can hold different data types and Python objects, so you can use . append() to add any object to a given list.


1 Answers

That's what they teach you in an algorithms and data structures class, that 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 a string
for i in [1, 2, 3]: # valid, using a list
for i in (1, 2, 3): # valid, using a 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.

like image 144
MrGeek Avatar answered Sep 29 '22 09:09

MrGeek