How to Convert String into List in Python?

By Hardik Savani October 30, 2023 Category : Python

Hello,

I will explain step by step tutorial python string into list. you'll learn how to convert string into list in python. let’s discuss about how to turn string into list in python. I would like to show you 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.

You can use these examples with python3 (Python 3) version.

let's see below a simple example with output:

Example 1:

main.py

myString = "ItSolutionStuff.com is a best site!"
  
# Convert String into List
newList = myString.split(" ")
    
print(newList)

Output:

['ItSolutionStuff.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 ItSolutionStuff.com"
  
# Convert String into List
newList = list(myString.strip(" "))
  
print(newList)

Output:

['S', 'i', 't', 'e', ' ', 'I', 't', 'S', 'o', 'l', 'u', 't', 'i', 'o', 'n', 'S', 't', 'u', 'f', 'f', '.', 'c', 'o', 'm']

I hope it can help you...

Tags :
Shares