Given a string, write a Python program to split the characters of the given string into a list using Python.
Examples:
Input : geeks Output : ['g', 'e', 'e', 'k', 's'] Input : Word Output : ['W', 'o', 'r', 'd']Method 1: Split a string into a Python list using unpack(*) method
The act of unpacking involves taking things out, specifically iterables like dictionaries, lists, and tuples.
Python3
string = "geeks"
print([*string])
Output:
['g', 'e', 'e', 'k', 's']Method 2: Split a string into a Python list using a loop
Here, we are splitting the letters using the native way using the loop and then we are appending it to a new list.
Python3
string = 'geeksforgeeks'
lst = []
for letter in string:
lst.append(letter)
print(lst)
Output:
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']Method 3: Split a string into a Python list using List Comprehension
This approach uses list comprehension to convert each character into a list. Using the following syntax you can split the characters of a string into a list.
Python3
string = "Geeksforgeeks"
letter = [x for x in string]
print(letter)
Output:
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']Method 4: Split a string into a Python list using a list() typecasting
Python provides direct typecasting of strings into a list using Python list().
Python3
def split(word):
return list(word)
word = 'geeks'
print(split(word))
Output:
['g', 'e', 'e', 'k', 's']Method 5: Split a string into a Python list using extend()
Extend iterates over its input, expanding the list, and adding each member.
Python3
string = 'Geeks@for'
lst = []
lst.extend(string)
print(lst)
Output:
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']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
def Convert(string):
li = list(string.split(" "))
return li
str1 = "Geeks for Geeks"
print(Convert(str1))
Output
['Geeks', 'for', 'Geeks']Example 1B:
Python3
def Convert(string):
li = list(string.split("-"))
return li
str1 = "Geeks-for-Geeks"
print(Convert(str1))
Output
['Geeks', 'for', 'Geeks']Method#2: Using string slicing
Python3
def Convert(string):
list1 = []
list1[:0] = string
return list1
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
import re
def Convert(string):
return re.findall('[a-zA-Z]', string)
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]'
res = json.loads(stringA)
print("The converted list : \n",res)
Output
The converted list : ['geeks', 2, 'for', 4, 'geeks', 3]Method #7: Using ast.literal
Python3
import ast
ini_list = '["geeks", 2,"for", 4, "geeks",3]'
res = ast.literal_eval(ini_list)
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']