Skip to content
Related Articles

Related Articles

Python | Program to convert String to a List

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 28 Sep, 2022
View Discussion
Improve Article
Save Article

In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the user’s choice, are encountered. To do this we use the split() method in string.

string.split("delimiter")

Examples:

Input : "Geeks for Geeks"
Output : ['Geeks', 'for', 'Geeks']
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']

Method#1: Using split() method

The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

Example 1A:

Python3




# Python code to convert string to list
 
 
def Convert(string):
    li = list(string.split(" "))
    return li
 
 
# Driver code
str1 = "Geeks for Geeks"
print(Convert(str1))


Output

['Geeks', 'for', 'Geeks']
 

Python-Foundation-Course

Example 1B:

Python3




# Python code to convert string to list
 
def Convert(string):
    li = list(string.split("-"))
    return li
 
 
# Driver code
str1 = "Geeks-for-Geeks"
print(Convert(str1))


Output

['Geeks', 'for', 'Geeks']

Method#2: Using string slicing

Python3




# Python code to convert string to list character-wise
 
def Convert(string):
    list1 = []
    list1[:0] = string
    return list1
 
 
# Driver code
str1 = "ABCD"
print(Convert(str1))


Output

['A', 'B', 'C', 'D']

Method#3: Using re.findall() method 

This task can be performed using regular expression. We can use the pattern to match all the alphabet and make list with all the matched elements. 

Python3




# Python code to convert string to list character-wise
# Using re.findall method
import re
 
# Function which uses re.findall method to convert string to list character wise
def Convert(string):
    return re.findall('[a-zA-Z]', string)
     
# Driver code
str1="ABCD"
print("List of character is : ",Convert(str1))


Output

List of character is :  ['A', 'B', 'C', 'D']

Method #4: Using list comprehension 

Python3




s="Geeks"
x=[i for i in s]
print(x)


Output

['G', 'e', 'e', 'k', 's']

Method #5: Using enumerate function 

Python3




s="geeks"
x=[i for a,i in enumerate(s) ]
print(x)


Output

['g', 'e', 'e', 'k', 's']

Method #6: Using JSON

Python3




import json
 
stringA = '["geeks", 2,"for", 4, "geeks",3]'
 
# Type check
 
res = json.loads(stringA)
# Result
print("The converted list : \n",res)


Output

The converted list : 
 ['geeks', 2, 'for', 4, 'geeks', 3]

Method #7: Using ast.literal

Python3




import ast
 
# initializing string representation of a list
ini_list = '["geeks", 2,"for", 4, "geeks",3]'
 
 
# Converting string to list
res = ast.literal_eval(ini_list)
 
# printing final result and its type
print(res)
print(type(res))


Output

['geeks', 2, 'for', 4, 'geeks', 3]
<class 'list'>

Method #8: Using lambda function

Python3




s="Geeks"
x=list(filter(lambda i:(i in s),s))
print(x)


Output

['G', 'e', 'e', 'k', 's']

Method #9: Using map() 

Python3




s="Geeks"
x=list(map(str,s))
print(x)


Output

['G', 'e', 'e', 'k', 's']

Method#10: Using list()

Python3




s = "Geeks"
x = list(s)
print(x)


Output:

['G', 'e', 'e', 'k', 's']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!