Print in a For loop in one line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 6, 2022

banner

Photo from Unsplash

Print in a For loop in one line in Python #

To print in a for loop in one line:

  1. Use a for loop to iterate over the sequence.
  2. Set the end argument of the print() function to an empty string.
  3. The items in the sequence will get printed on the same line.
main.py
my_list = ['bobby', 'hadz', '.com'] # ✅ print in a for loop in one line without a separator for item in my_list: print(item, end='') # 👉️ bobbyhadz.com # ✅ print in a for loop in one line with a space separator for item in my_list: print(item, end=' ') # 👉️ bobby hadz .com # --------------------------- # ✅ print in one line using str.join() result = ''.join(my_list) print(result) # 👉️ bobbyhadz.com

The first example uses the end argument to print in a for loop in one line.

The end argument is printed at the end of the message. By default, end is set to a newline character (\n).

main.py
for item in ['bobby', 'hadz']: # bobby hadz print(item, end=' ') for item in ['bobby', 'hadz']: # bobby # hadz print(item)

You can set the end argument to an empty string or a string containing a space.

The value of the end argument gets printed immediately after the message you pass to the print() function.

The print() function also takes a sep argument.

The sep argument is the separator between the arguments we pass to print().

main.py
print('bobby', 'hadz', sep='') # 👉️ bobbyhadz print('bobby', 'hadz') # 👉️ bobby hadz

By default, the sep argument is set to a space.

You can set it to an empty string to print without spaces between the values.

If you just need to print the items in a sequence in one line, you can also use the str.join() method.

main.py
my_list = ['bobby', 'hadz', '.com'] result = ''.join(my_list) print(result) # 👉️ bobbyhadz.com

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your iterable contains numbers or other types, convert all of the values to strings before calling join().

main.py
my_list = [13, 5, 30] result = ''.join(str(item) for item in my_list) print(result) # 👉️ 13530

The string the method is called on is used as the separator between the elements.

main.py
my_list = ['bobby', 'hadz', '.com'] result = ' '.join(my_list) print(result) # 👉️ bobby hadz .com

If the items in your iterable contain trailing or leading newline characters, use the str.strip() method to remove them.

main.py
my_list = ['bobby\n', 'hadz\n', '.com\n'] list_without_newlines = [item.strip() for item in my_list] print(list_without_newlines) # 👉️ ['bobby', 'hadz', '.com'] for item in list_without_newlines: print(item, end='') # bobbyhadz.com

We used a list comprehension to iterate over the list and removed the leading and trailing newline (\n) characters from each item.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.strip() method to remove the leading and trailing newline characters from the current string and return the result.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.