Home  >  Article  >  Backend Development  >  How to convert python string to list

How to convert python string to list

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-22 11:23:0757165browse

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.

How to convert python string to list

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is list in pythonNext article:What is list in python