How to Print a List of Space-Separated Elements in Python?

Estimated read time 1 min read

To print a list of space-separated elements in Python, you can use a loop to iterate through each element in the list and print each element using the print() function. By default, the print() function separates arguments with a space, so you can simply pass the list as an argument to the print() function.

Here is an example:

my_list = [1, 2, 3, 4, 5]

print(*my_list)

In this example, we have a list called my_list with five integer elements. We use the print() function with the * operator to unpack the list and print each element as a separate argument. Since the print() function separates arguments with a space by default, this will print the elements of the list separated by spaces.

The output of this code will be:

1 2 3 4 5

Note that you can customize the separator between elements by passing the sep parameter to the print() function. For example, you can use print(*my_list, sep=", ") to separate the elements with a comma and a space instead of a space only.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply