Given a string, write a Python program to split the characters of the given string into a list using Python. In this article, we will explore various methods to split a string into a list of characters, allowing developers to manipulate and work with individual characters efficiently.
Input: geeks
Output : ['g', 'e', 'e', 'k', 's']
Input: Word
Output : ['W', 'o', 'r', 'd']
Explanation: In this, we are splitting the string into a list of characters in Python.
Python Split String in List Method
Below is the list of methods that we will use in this article:
- Using unpack(*) method
- Using a loop
- Using List Comprehension
- Using list() typecasting
- Using extend() function
- Using itertools
- Using List Slicing
Python Split String in 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']
Time complexity: O(n), where n is the length of the input word.
Auxiliary space: O(n)
Python Split String in 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']
Time complexity: O(n), where n is the length of the input word.
Auxiliary space: O(n)
Python Split String in 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']
Time complexity: O(n), where n is the length of the input word.
Auxiliary space: O(n)
Python Split String 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']
Time complexity: O(n), where n is the length of the input word.
Auxiliary space: O(n), where n is the length of the input word.
Python Split String in List using Extend() Function
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']
Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(n), where n is the length of the string.
Python Split String using Itertools
In Python, you can split a string into a list of characters using the itertools module. You need to convert the string into an iterable sequence (e.g., a list, tuple, or string itself) of individual characters
Python3
import itertools
string = "Geeksforgeeks"
letter = list (itertools.chain.from_iterable(string))
print (letter)
|
Output
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
The time complexity : O(n), where n is the length of the input string.
The auxiliary space : O(n), as the list created by itertools.chain.from_iterable has a size of n.
Python Split String in List using List Slicing
In Python, you can split a string into a list of characters using list slicing. List slicing is a technique that allows you to extract a portion of a list (or string) by specifying the start and end indices.
Python
string = 'Geeks@for'
lst = []
lst[:] = string
print (lst)
|
Output
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
Time complexity: O(N), where N is the length of the input string.
Auxiliary space: O(N), Because a new list of N size is created.
Last Updated :
01 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...