How to Split a String into a List of Letters

5/5 - (1 vote)

Problem Formulation and Solution Overview

In this article, you’ll learn how to split a string into a List of Letters in Python.

To make it more fun, we have the following running scenario:

The Finxter Academy would like a text version of their logo by splitting the string into a List of Elements and applying different colors to each letter.
Let’s start off by splitting this string into a List of Letters.

💬 Question: How would we write Python code to split the string into a List of Letters?

We can accomplish this task by one of the following options:


Method 1: Use list()

This example uses a List to take a string and split each letter into its own element, thus creating a List of Letters.

slogan  = 'Finxter'
letters = list(slogan)
print(letters)  

This code declares the variable slogan. This variable is passed as a parameter to the list. An iterable is created and saved to letters.
The output is then sent to the terminal.

Output

['F', 'i', 'n', 'x', 't', 'e', 'r']

Method 2: Use split()

If the initial string contains separating letters such as a hyphen (-) or another letter, use split() with a parameter to create a List of Letters.

slogan  = 'F-i-n-x-t-e-r'
letters = slogan.split('-')
print(letters)     

This code declares the variable slogan. The split() method is appended to the end of the slogan variable. Then split() is passed the separating letter, in this case, the hyphen (-). The results save to letters and the output is sent to the terminal.

Output

['F', 'i', 'n', 'x', 't', 'e', 'r']

Method 3: Use shlex.split()

This example calls the shlex library, which uses its split() function. The initial string must be separated by spaces to create a List of Letters.

import shlex

slogan = 'F i n x t e r'
letters = shlex.split(slogan)
print(letters) 

This code declares the variable slogan. The split() method is appended to the end of the slogan variable. Then shlex.split() is passed slogan and split on the space (' ') letter. The results save to letters and the output is sent to the terminal.

Output

['F', 'i', 'n', 'x', 't', 'e', 'r']

Method 4: Use List Comprehension

This example uses List Comprehension to split a string into a List of Letters. A clean, readable way to perform this task.

slogan  = 'Finxter'
letters = [x for x in slogan]
print(letters)

⭐A Finxter Favorite!

This code uses loops through each letter in the variable slogan. Each letter saves to letters: one per element. The output is then sent to the terminal.

Output

['F', 'i', 'n', 'x', 't', 'e', 'r']

Bonus: Color Letters

In this example, we will run our code from an IDE terminal and color each letter to match the Finxter Logo as closely as possible.

from colorama import Fore, init
init(autoreset=True)

slogan  = 'Finxter'
letters = [x for x in slogan]
tmp = ""

for i, x in enumerate(letters):
    if i == 0 or i == 4: 
        letters[i] = Fore.GREEN + str(x) + ' '
    elif i == 1 or i == 5:
        letters[i] = Fore.BLUE + str(x) + ' '
    elif i == 2 or i == 6:  
        letters[i] = Fore.RED + str(x) + ' '
    elif i == 3:
        letters[i] = Fore.YELLOW + str(x) + ' '
    else: 
        letters[i] = str(x)
    tmp += letters[i]
print(tmp)

This code uses the Python Colorama library, which provides the ability to print colored text in Python. However, this library only offers 16 color choices.

Output

Not bad, but the colors need to be tweaked. To learn how to address this issue, stay tuned for our article on How to Print Colored Text in Python.


Summary

These four (4) methods of converting a String into a List of Letters should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!