Home  >  Article  >  Backend Development  >  How to remove square brackets from data in python

How to remove square brackets from data in python

(*-*)浩
(*-*)浩Original
2019-07-01 13:24:1511218browse

Usually when Python outputs a list string, it will automatically add quotation marks and brackets.

How to remove square brackets from data in python

For example (recommended learning: Python video tutorial)

str=['hello','world']
>>> str
['hello', 'world']

Method 1

You can use the join method:

>>> print " ".join(str)
hello world

Among them: The Python join() method is used to connect the elements in the sequence with the specified characters to generate a new string.

For example:

str = "-"
seq = ("a", "b", "c")
print str.join( seq )

Output result:

a-b-c

Method 2: If the list stores numbers, then: just convert it to int. Because the original storage is string type

str=['1', '2', '3']
for i in str:    
    int(i)  
    print(i)

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to remove square brackets from data in python. 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