Converting string into a list

In this blog, we will create simple program that will take any string as input from user and convert it into a list. Making each character of a string as an individual element in a list

Asking user to input any string

str = input('Enter a string: ')

Creating empty list

strtolist = []

Using for loop to access each character of a string and then adding it to end of the list using append method

for s in str:
strtolist.append(s)

Display elements in a list

print(strtolist)

Output

Enter a string: Python
['P', 'y', 't', 'h', 'o', 'n']

Complete Code

str = input('Enter a string: ')
strtolist = []
for s in str:
    strtolist.append(s)
print(strtolist)
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s