2 Easy Ways to Convert List to String in Python

You can convert a list into a string using for-loop and join methods in python. A list is a group of data. That can be numbers, words, or letters. These individual values you can convert as a string.

String-list-python

IN THIS PAGE

  1. Python List
  2. Python print list as string
    1. Way#1: For-loop method
    2. Way#2: Join method

Python List

Creating list

The first step is to create a list. Here the object list_of_values assigns to the list. The list has data decimals, numbers, and words.

list_of_values = [1.0, 'this is a test', 2, 'c', 'hello world']
Advertisements

Print and check each value in the list

The programs uses for-loop to print each value of the list.

list_of_values = [1.0, 'this is a test', 2, 'c', 'hello world']
for v in list_of_values:
  print(v)

Output

1.0 
this is a test 
2 
c 
hello world 

** Process exited - Return Code: 0 ** 
Press Enter to exit terminal

Python print list as string

In two ways you can print list as string. One is by using for loop, and the other is by using the Join method.

Way#1: For-loop method

list_of_values = [1.0, 'this is a test', 2, 'c', 'hello world']
for v in list_of_values:
  print(v, end=' ')  # Space included in end as separator  

Output

1.0 this is a test 2 c hello world 

** Process exited - Return Code: 0 ** 
Press Enter to exit terminal

Way#2: Join method

Using join method and print, you can build a string.

list_of_values = [1.0, 'this is a test', 2, 'c', 'hello world']
s = ' '.join([str(i) for i in list_of_values])
print(s)

Output

1.0 this is a test 2 c hello world 
** Process exited - Return Code: 0 ** 
Press Enter to exit terminal

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.