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.
IN THIS PAGE
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']
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
-
5 Top Insurance Domain Books to Read Right Now
Here are the five top books on the insurance domain that help you know the business process right now.
-
Python JSON Dump Vs. Load: What’s the Difference
Here are the differences between JSON dump vs. load vs. loads in Python. Here is how these work each other well explained.
-
SQL Query: How to Check Index Space
Here are the top SQL queries how used to know index size. Explained views to refer to as a user /or as a DBA.