How to convert a string to list of characters?
Convert a String to List of Character
Given a string myStr
, we have to convert the string myStr
to a list, where the elements of the list are the characters in the string, in respective order.
To convert the given string into a list of characters in Python, use List Comprehension with the given string as iterable. Since the string is an iterable of characters, the list comprehension with the string returns an list of characters.
Syntax
The syntax of the expression to convert a string myStr
into a list of characters using List Comprehension is
[*myStr]
Program
In the following program, we take a string myStr
and convert this string into a list of characters.
Python Program
myStr = 'apple'
arr = [*myStr]
print(arr)
Output
['a', 'p', 'p', 'l', 'e']
Summary
In this tutorial of Python Examples, we learned how to convert a string to a list of characters using List Comprehension, with the help of well detailed examples.