How to split a word in Python | Example code

Do typecast to convert a string into the list of char. For it use the list() function to split a word into a list of letters in Python.

How to split a word in Python example

Simple example code splitting a word into a list of char creates a list.

word = "word"

res = list(word)

print(res)

Output:

How to split a word in Python

Is there a function in python to split a word into a list?

The easiest way is probably just to use list(), but there is at least one other option as well:

s = "Word to Split"

wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension

print(wordlist)

Output: [‘W’, ‘o’, ‘r’, ‘d’, ‘ ‘, ‘t’, ‘o’, ‘ ‘, ‘S’, ‘p’, ‘l’, ‘i’, ‘t’]

Do comment if you have any doubts and suggestions on this Python split topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.