To convert a string to a list in Python, you can use the split() method. This method splits a string into a list of substrings based on a specified delimiter.

Here's an example:

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

Output:

['Hello,', 'World!']

In the example above, the split() method is used without any delimiter specified. By default, it splits the string based on whitespace characters (spaces, tabs, and newlines). However, you can also pass a specific delimiter as an argument to the split() method. For example, if you want to split the string at commas, you can use split(",").

string = "apple,banana,orange"
list = string.split(",")
print(list)

Output:

['apple', 'banana', 'orange']
``
python str to list

原文地址: https://www.cveoy.top/t/topic/hAXk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录