Python provides a list structure in order to store multiple items in a single variable. These variables can be different types like string, integer, object, etc. A list can be printed by using different ways in Python. Printing a list means putting item values into the standard output like a terminal, web page, or file. The list is very similar to the array in other programming languages and printing the list operation is also called a printing array in Python.
Print List with for Loop
The most popular and traditional way to print a list is using the for a loop. The for loop is used to iterate over all elements of the list items and these items are printed by using the print() method.
countries = ["usa","germany","turkey","spain"]
for item in countries:
print(item)
Print List with print() Method
Another easy way to print a list is using the print() method which is very useful. Also, the * operator is used to export all items of the list. All printed items are separated with spaces.
countries = ["usa","germany","turkey","spain"]
print(*a)
Alternatively, we can specify a different separator than space. The command can be used as a separator for the printed items.
countries = ["usa","germany","turkey","spain"]
print(*a,sep=", ")
Print List with join() Method
The join() method is used to put multiple items into a single string by joining them. The string type provides the join() method which accepts a list as a parameter and all items are put together by using string value as the separator.
countries = ["usa","germany","turkey","spain"]
print(' '.join(countries))
Print List Using map() Function
The map()
function converts every item of the list into a string if the list is not a string. Then the join()
function can be used to join them together. We also provide the conversion type as str
to the map() function.
numbers= [1,2,3,4,5,6,7,8,9]
print('\n'.join(map(str,numbers)))