An easy way to convert a python list to a string

An easy way to convert a python list to a string

If you use the str() function to convert the list directly to a string, the string will contain symbols such as "[", "]" and ",", as in the example below:

>>> str([1,2])
'[1, 2]'

So how to concatenate the elements in the list and return it as a string type value? You can use python's built-in join() method. Before using it, you need to convert each element to a string type. You can use the map() function and str() function to complete it. See the example below for details.

Example code for converting python list to string

In the following example, an empty string is used to connect the elements in the list of symbolic links:

>>> "".join(map(str,[1,2,3,4,5,6,7,8,9,"笨鸟工具,x1y1z1.com"]))
'123456789笨鸟工具,x1y1z1.com'

Original post: An easy way to convert a python list to a string

Guess you like

Origin blog.csdn.net/weixin_47378963/article/details/130235080