Sometimes we need to work with just the lists and hence strings might need to be converted into lists. It has to be converted in list of characters for certain tasks to be performed. This is generally required in Machine Learning to preprocess data and text classifications. Let’s discuss certain ways in which this task is performed.
Method #1: Using list slicing
List slicing can be used for this particular purpose, in which we assign to each index element of list the next occurring character of string using the slice operation.
Python3
test_string = "GeeksforGeeks"
print ( "The original string is : " + str (test_string))
res = []
res[:] = test_string
print ( "The resultant list of characters : " + str (res))
|
Output
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
The time complexity of this code is O(n), where n is the length of the input string,
The auxiliary space complexity of this code is also O(n), because a new list of length n is created to store the characters from the input string.
Method #2: Using list()
The most concise and readable way to perform splitting is to type case string into list and the splitting of list is automatically handled internally. This is recommended method to perform this particular task.
Python3
test_string = "GeeksforGeeks"
print ( "The original string is : " + str (test_string))
res = list (test_string)
print ( "The resultant list of characters : " + str (res))
|
Output
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Time Complexity: O(n) where n is the length of the input string.
Auxiliary Space: O(n)
Method #3: Using map() + lambda
This is yet another way to perform this particular task. Though not recommended but can be used in certain situations. But drawback is readability of code gets sacrificed.
Python3
test_string = "GeeksforGeeks"
print ( "The original string is : " + str (test_string))
res = list ( map ( lambda i:i, test_string))
print ( "The resultant list of characters : " + str (res))
|
Output
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Method#4: Using join() + split() method
We can use above mention method to split the string in list of character. Join is used to add the space between the character of string and split is used to split the character between space in string.
Python3
test_string = "GeeksforGeeks"
print ( "The original string is : " + str (test_string))
test_string = " " .join(test_string)
res = test_string.split( " " )
print ( "The resultant list of characters : " + str (res))
|
Output
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Method#5: Using re.findall() method: We can use above mention method to find all character in the string and form a list of characters.
Python
import re
test_string = "GeeksforGeeks"
print ( "The original string is : " + str (test_string))
res = re.findall(r '[a-zA-z]' , test_string)
print ( "The resultant list of characters : " + str (res))
|
Output
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Method #6 : Using for loop
Python3
test_string = 'GeeksforGeeks'
print ( "The original string is : " + str (test_string))
res = []
for i in test_string:
res.append(i)
print ( "The splitted character's list is : " + str (res))
|
Output
The original string is : GeeksforGeeks
The splitted character's list is : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Apr, 2023
Like Article
Save Article