How do you convert a string to a list in Python?

In Python, you can convert a string to a list using the list() function. This function will treat each character in the string as an element in the list.

Here is an example:

string = "Hello, World!"
list = list(string)
print(list)

Output:

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

You can also achieve the same effect using list comprehension.

string = "Hello, World!"
list = [char for char in string]
print(list)

Output:

['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
广告
Closing in 10 seconds
bannerAds