Open In App

Python | Split string into list of characters

Last Updated : 01 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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. 



Similar Reads

Python | Pandas Split strings into two List/Columns using str.split()
Pandas provide a method to split string around a passed separator/delimiter. After that, the string can be stored as a list in a series or it can also be used to create multiple column data frames from a single separated string. It works similarly to Python's default split() method but it can only be applied to an individual string. Pandas <code
4 min read
Python | Convert list of strings and characters to list of characters
Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Let's discuss certain ways in which this is achieved. Method #1 : Using List comprehension In this method, we just consider each
6 min read
Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas provide a method to split string around a passed separator or delimiter. After that, the string can be stored as a list in a seri
3 min read
Python | Split a sentence into list of words
Given a Sentence, write a Python program to convert the given sentence into a list of words. Examples: Input : 'Hello World' Output : ['Hello', 'world']Method 1: Split a sentence into a list using split() The simplest approach provided by Python to convert the given list of Sentences into words with separate indices is to use split() method. This m
5 min read
Python | Split a list into sublists of given lengths
The problem of splitting a list into sublists is quite generic but to split in sublist of given length is not so common. Given a list of lists and list of length, the task is to split the list into sublists of given length. Example: Input : Input = [1, 2, 3, 4, 5, 6, 7] length_to_split = [2, 1, 3, 1] Output: [[1, 2], [3], [4, 5, 6], [7]] Method #1:
2 min read
Python | Split list of strings into sublists based on length
Given a list of strings, write a Python program to split the list into sublists based on string length. Examples: Input : ['The', 'art', 'of', 'programming'] Output : [['of'], ['The', 'art'], ['programming']] Input : ['Welcome', 'to', 'geeksforgeeks'] Output : [['to'], ['Welcome'], ['geeksforgeeks']] Approach #1 : Naive A naive approach for the abo
3 min read
Python | Split list into lists by particular value
In Python, To split a list into sublists based on a particular value. The idea is to iterate through the original list and group elements into sub-lists whenever the specified value is encountered. It is often necessary to manipulate and process lists, especially when dealing with large amounts of data. One frequent operation is dividing a list int
5 min read
Python - Split list into all possible tuple pairs
Given a list, the task is to write a python program that can split it into all possible tuple pairs combinations. Input : test_list = [4, 7, 5, 1, 9] Output : [[4, 7, 5, 1, 9], [4, 7, 5, (1, 9)], [4, 7, (5, 1), 9], [4, 7, (5, 9), 1], [4, (7, 5), 1, 9], [4, (7, 5), (1, 9)], [4, (7, 1), 5, 9], [4, (7, 1), (5, 9)], [4, (7, 9), 5, 1], [4, (7, 9), (5, 1
3 min read
How to Split a File into a List in Python
In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples to see how it's done. Example 1: Using the splitlin
5 min read
Python | Split nested list into two lists
Given a nested 2D list, the task is to split the nested list into two lists such that the first list contains the first elements of each sublist and the second list contains the second element of each sublist. In this article, we will see how to split nested lists into two lists in Python. Python Split Nested List into Two ListsBelow are the ways b
5 min read