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
-
How to Count and Print Blank Spaces in Python
Two Python methods that show you how to count blank spaces in a text file
-
The Easy Way to Get Duplicate Records: Top SQL Query
Here is an SQL query explained to get duplicate records from a table
-
How to write Stored Procedure in DB2: Unique Ideas
The unique tips you must know before you write a high-performing stored procedure in DB2