How to Split string into list of characters in Python

Python split string into list of characters

In this article, we will be solving a program of python split string into list of characters. We will be discussing 4 methods to do this particular task.

In Python, the easiest way to convert the string in list of characters is by using a python inbuilt function list(). By using list() the string is converted into a list that contains all the characters of the given string.

list() – This method returns a list in Python.

Syntax: list(iterable_object)

The iterable object can be collection (set, dictionary) or it could be a sequence like string or tuple.

Python Program:

#input string
input_string = "ProblemSeeker"
# list() is used to split string into list of charcters
output_list = list(input_string)
print("The input string is:",input_string)
print("The output list of characters is:")
print(output_list)

Output:
The input string is: ProblemSeeker
The output list of characters is:
[‘P’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’, ‘S’, ‘e’, ‘e’, ‘k’, ‘e’, ‘r’]

Method 2- Python Split String into list of characters using For loop and append()

In this method, we will be using a for loop to iterate within the characters for of string and then add character by character to the list. To add the character or any element in a list we use the append method.

append() method: To add an element at the end of the list we use the append() method. 

Syntax:  list_name.append(element)

Here the element can be any data type (string, float, int, list, etc)

Python Program

#input string
input_string = "ProblemSeeker"
#initialization of output list
output_list = []
for character in input_string: #for loop
    #adding each character to list using append function
    output_list.append(character) 
    
print("The input string is :",input_string)
print("The output list of characters is:")
print(output_list)

Output:
The input string is : ProblemSeeker
The output list of characters is:
[‘P’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’, ‘S’, ‘e’, ‘e’, ‘k’, ‘e’, ‘r’]

Method 3- Split String into list of characters using list Comprehension

This method is similar to the for loop method but it is shortened and also we don’t use the append() method here as we are using list comprehension.

To know about the list comprehension you can read about it from here.

Python Program

#input string
input_string = "ProblemSeeker"
#using list comprehension
output_string = [character for character in input_string]
   
print("The input string is :",input_string)
print("The output list of charcters is:")
print(output_list)

Output:
The input string is : ProblemSeeker
The output list of characters is:
[‘P’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’, ‘S’, ‘e’, ‘e’, ‘k’, ‘e’, ‘r’]

Method 4- Split String into list of characters using map and lambda

Another way to do this particular task is by using the map and lambda function. To get the concept of map and lambda you can read it from here.

Python Program

#input string
input_string = "ProblemSeeker"
#using map and lambda function
output_list = list(map(lambda character: character, input_string))    
print("The input string is :",input_string)
print("The output list of charcters is:")
print(output_list)

Output:
The input string is : ProblemSeeker
The output list of characters is:
[‘P’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’, ‘S’, ‘e’, ‘e’, ‘k’, ‘e’, ‘r’]

Conclusion

Hence by using For loop, list() method, list comprehension, and map and lambda we can split string into list of characters in Python.