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

How to convert list to string in python

青灯夜游
青灯夜游Original
2021-05-10 15:55:44106261browse

In python, you can use the join() method to convert a list into a string; the join() method is used to connect the elements in the list with specified characters to generate a new string, the syntax format is "' Delimiter '.join(list)".

How to convert list to string in python

The operating environment of this tutorial: windows7 system, python3.7 version, DELL G3 computer

In python, you can use the join() method Convert list to string.

Command: ''.join(list)

Among them, the quotation marks are the separators between characters, such as ",", ";", "\ t" and so on

For example:

list = [1, 2, 3, 4, 5]

''.join(list) The result is: 12345

','.join(list) The result is: 1 ,2,3,4,5

Example:

 str=[]  #有的题目要输出字符串,但是有时候list更好操作,于是可以最后list转string提交
 for i in range(0,a):
     str.append('M')              
 str1=''.join(str)

[Related recommendations: Python3 video tutorial]

The above is the detailed content of How to convert list to string 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