Contents
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)
Run 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.
Related Tutorials
- How to Append List to Another List in Python? – list.extend(list)
- Python – Check if Element is in List
- How to Sort Python List?
- How to Get List of all Files in Directory and Sub-directories?
- Python – List of Strings
- Python List of Lists
- Python List with First N Elements
- Python Program to Find Unique Items of a List
- How to Check if Python List is Empty?
- Python – Check if List Contains all Elements of Another List