How to Create a List of Characters in Python?

Estimated read time 1 min read

In Python, you can create a list of characters by enclosing the characters within square brackets [] and separating them using commas. Here’s an example:

my_list = ['a', 'b', 'c', 'd']

In this example, we have created a list of characters ‘a’, ‘b’, ‘c’, and ‘d’ and assigned it to the variable my_list.

You can also create a list of characters from a string using the list() function. Here’s an example:

my_string = "hello"
my_list = list(my_string)

In this example, we have created a string ‘hello’ and assigned it to the variable my_string. We then converted this string to a list of characters using the list() function and assigned it to the variable my_list.

You can 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 = ['a', 'b', 'c', 'd']
print(my_list[0]) # Output: 'a'
print(my_list[1]) # Output: 'b'
print(my_list[2]) # Output: 'c'
print(my_list[3]) # Output: 'd'

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply