How to Create a List of Characters from a String in Python?

Estimated read time 1 min read

In Python, you can create a list of characters from a string using the list() function. The list() function converts a string into a list of individual characters.

Here’s an example:

my_string = "Hello World"
my_list = list(my_string)

In this example, we have created a string “Hello World” and assigned it to the variable my_string. We then passed this string to the list() function, which converts it to a list of individual characters and assigns it to the variable my_list.

The output of the print() statement would be:

['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

This is a list of individual characters in the original string.

You can also access individual characters in a list using their index. The index of the first element in a list is 0. Here’s an example:

my_list = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
print(my_list[0]) # Output: 'H'
print(my_list[1]) # Output: 'e'
print(my_list[2]) # Output: 'l'
print(my_list[3]) # Output: 'l'

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply