How To Split A Word Into A List Of Letters In Python

Split a word into a list of letters in Python

To split a word into a list of letters in Python, you can use list(), append(), extend() functions and for loop. Follow the article to understand better.

Split a word into a list of letters in Python

In Python, ‘list’ is a data type that allows storing various data types inside it and retrieving their values through the position of the elements. In Python, you can say ‘list’ is the most flexible data type.

To split a word into a list of letters in Python, I have the following ways:

Use the list() function

In this method list() function will split each word into a list of independent letters.

Syntax:

list([iterable])

Parameters:

  • iterable: It can be a string, tuple, set, dictionary or iterator object.

Example:

strMesg = 'Visit learnshareIT website'

# Use the list() function to split that string into a list of letters
print(list(strMesg))

Output:

['V', 'i', 's', 'i', 't', ' ', 'l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'I', 'T', ' ', 'w', 'e', 'b', 's', 'i', 't', 'e']

Use the extend() method

The extend() method in Python is used to add elements of an iterable (an object containing many elements) such as a list, tuple, or string to the end of a list in Python and expand that list.

Syntax:

list.extend(iterable)

Parameters:

  • list: is the original list.
  • iterable: is the object containing the elements to be added to the original list.

Example:

  • Create a string.
  • Initialize an empty array to store the list of letters after splitting.
  • Use extend() function to split.
  • Print out the list initialized in step 2.
strMesg = 'Visit learnshareIT website'
emptyList = []

# Use extend() function to split
emptyList.extend(strMesg)
print(emptyList)

Output:

['V', 'i', 's', 'i', 't', ' ', 'l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'I', 'T', ' ', 'w', 'e', 'b', 's', 'i', 't', 'e']

Use For loop

The for loop is also used to split a word into a list of letters.

Example:

  • Create a string.
  • Use For loop to split that string into a list of letters.
strMesg = 'Visit learnshareIT website'
result = [l for l in strMesg]

print(result)

Output:

['V', 'i', 's', 'i', 't', ' ', 'l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'I', 'T', ' ', 'w', 'e', 'b', 's', 'i', 't', 'e']

Use the append() method

The append() method in Python is used to add an element to the end of a list in Python. We use append to add a number, a list, a string or a tuple as an element to the end of a python list.

Syntax:

list.append(element)

Parameters:

  • list: is the initial list.
  • element: is the element to be added.

Example:

  • Create a string.
  • Initialize an empty array to store the list of letters after splitting.
  • Use append() function to split.
  • Print out the list initialized in step 2.
strMesg = 'Visit learnshareIT website'
emptyList = []

for i in strMesg:
   emptyList.append(i) # Use append() function to split

print(emptyList)

Output:

['V', 'i', 's', 'i', 't', ' ', 'l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'I', 'T', ' ', 'w', 'e', 'b', 's', 'i', 't', 'e']

Summary

If you have any questions about how to split a word into a list of letters in Python, leave a comment below. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *