Home > database >  How to add a whole sentence to a list in python
How to add a whole sentence to a list in python

Time:10-13

I want to add a whole sentence inside a list in python but when I give :

name = "My name is steven"
name = list(name)
print(name)

The output is:

['M', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 's', 't', 'e', 'v', 'e', 'n']

but I want it in the form of :

["My name is steven"]

further I have to add other sentences also inside list. What can I do?

CodePudding user response:

You are passing iterable to list so it will convert string to list of characters in string

name = "My name is steven"
name = [name]  # This way you can convert it into desired output
print(name)

CodePudding user response:

I would recommend using .append especially if you have additonal sentences to add.

a=[]
name="My name is Steven"
a.append(name)
# a=["My name is Steven"]
  • Related