Home > Article > Backend Development > How to convert python string to list
String is the most commonly used data type in Python. We can create a string using quotes (' or "). Creating a string is as simple as assigning a value to a variable. Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number - Its position, or index, is 0 for the first index, 1 for the second, and so on.
Python has 6 built-in types for sequences, but the most common are lists and tuples. Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods for determining the length of the sequence and determining the largest and smallest elements. method. Lists are the most commonly used Python data type and can appear as comma-separated values within square brackets. The data items of a list do not need to be of the same type. To create a list, simply separate comma-separated data items using Just enclose it in parentheses.
Related recommendations: "Python Video Tutorial"
str1 = "12345" list1 = list(str1) print list1 str2 = "123 sjhid dhi" list2 = str2.split() #or list2 = str2.split(" ") print list2 str3 = "www.google.com" list3 = str3.split(".") print list3
The results are as follows:
['1', '2', '3', '4', '5'] ['123', 'sjhid', 'dhi'] ['www', 'google', 'com']
The Python strip() method uses To remove the specified characters at the beginning and end of a string
split() is to split a string into a list of multiple strings
>>> image ='1.jsp,2.jsp,3.jsp,4.jsp' >>> image_list = image.strip(',').split(',') >>> print image_list ['1.jsp', '2.jsp', '3.jsp', '4.jsp'] >>>
The above is the detailed content of How to convert python string to list. For more information, please follow other related articles on the PHP Chinese website!