Convert String into List in Python Example

28-Oct-2022

.

Admin

Convert String into List in Python Example

Hi Dev,

Are you looking for example of Convert String into List in Python Example. I explained simply about python string into list of characters. Here you will learn how to convert string into list in python without split. This post will give you simple example of how to convert string into list of words in python.

There are several ways to convert strings into a list in python. we will use split() and strip() functions to convert string into list. so let's see the below examples.

so let's see following examples with output:

Example 1:


main.py

myString = "Nicesnippets.com is a best site!"

# Convert String into List

newList = myString.split(" ")

print(newList)

Output:

['Nicesnippets.com', 'is', 'a', 'best', 'site!']

Example 2:

main.py

myString = "One,Two,Three,Four,Five"

# Convert String into List

newList = myString.split(",")

print(newList)

Output:

['One', 'Two', 'Three', 'Four', 'Five']

Example 3:

main.py

myString = "Site Nicesnippets.com"

# Convert String into List

newList = list(myString.strip(" "))

print(newList)

Output:

['S', 'i', 't', 'e', ' ', 'N', 'i', 'c', 'e', 's', 'n', 'i', 'p', 'p', 'e', 't', 's' , '.', 'c', 'o', 'm']

#Python