Best Solution to Print List Without Newline
To print all values of a Python list without the trailing newline character or line break, pass the
end='' argument in your
print() function call. Python will then avoid adding a newline character after the printed output. For example, the expression
print(*[1, 2, 3], end='') will print without newline.
lst = [1, 2, 3] print(*lst, end='') # 1 2 3
lst = [1, 2, 3] print(*lst, end='') print('hi') # 1 2 3hi
This code snippet, short as it is, uses a couple of important Python features. Check out the following articles to learn more about them:
Print List Without Newline and With Separators
To print all values of a Python list without newline character pass the
end='' argument in your
print() function call. To overwrite the default empty space as a separator string, pass the sep=’…’ argument with your separator. For example, the expression
print(*[1, 2, 3], sep='|', end='') will print without newline.
lst = [1, 2, 3] print(*[1, 2, 3], sep='|', end='') print('hi') # 1|2|3hi
Also, check out the following video tutorial on printing something and using a separator and
Python Print Raw List Without Newline
If you don’t want to print the individual elements of the list but the overall list with square brackets and comma-separated, but you don’t want to print the newline character afterwards, the best way is to pass the list into the
print() function along with the
end='' argument such as in
print([1, 2, 3], end='').
lst = [1, 2, 3] print([1, 2, 3], end='') print('hi') # [1, 2, 3]hi
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.