Appending a string in python -


why statement produce error though string list of character constants?

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

the reason consider true because when use loop, allowed use statement-

for in string_name:
......

i think string_name considered list here

that's teach in algorithms , data structures class, deal algorithmic languages (unreal) rather real programming languages, in python, string string, , list list, they're different objects, can "append" string using called string concatenation:

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

or shorthand concatenation:

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

lists , strings belong type called iterable. iterables they're name suggests, iterables, meaning can iterate through them key word in, doesn't mean they're same type of objects:

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

i recommend read python documentation and/or take python's tutorial.

from docs glossary:

iterable
object capable of returning members 1 @ time. examples of iterables include sequence types (such list, str, , tuple) , non-sequence types dict, file objects, , objects of classes define __iter__() or __getitem__() method. iterables can used in loop , in many other places sequence needed (zip(), map(), …). when iterable object passed argument built-in function iter(), returns iterator object. iterator 1 pass on set of values. when using iterables, not necessary call iter() or deal iterator objects yourself. statement automatically you, creating temporary unnamed variable hold iterator duration of loop. see iterator, sequence, , generator. more iterables.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -