Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python | Split string into list of characters

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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)
     
# Driver code
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.

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']

Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(n), where n is the length of the string.

Method 6: Using  itertools:

Algorithm :

  1. Import the itertools module.
  2. Initialize a string “Geeksforgeeks”.
  3. Call the itertools.chain.from_iterable method with the string as the argument. This method returns an iterator that chains the elements of the input iterable (in this case, the string) together into a single sequence.
  4. Convert the iterator to a list and store the result in the variable letter.
  5. Print the value of letter.

Python3




import itertools
 
string = "Geeksforgeeks"
letter = list(itertools.chain.from_iterable(string))
print(letter)
#This code is contributed by Jyothi pinjala.

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.

Method 7: Using list slicing

Approach: 

  • Initialize a string “Geeks@for”.
  • Print the original string. 
  • Use list slicing for splitting a string into a list of characters.
  • Print the value of the list.

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. 


My Personal Notes arrow_drop_up
Last Updated : 02 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials