Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Print a List Without Newline in Python?

#1
How to Print a List Without Newline in Python?

Rate this post

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

If you print anything afterward, it’ll be added right to the list.

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 end argument:

YouTube Video

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


https://www.sickgaming.net/blog/2022/09/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] List Comprehension in Python xSicKxBot 0 2,104 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,954 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,555 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,694 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,606 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,509 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,529 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] Python | Split String and Remove newline xSicKxBot 0 1,292 12-16-2022, 10:38 PM
Last Post: xSicKxBot
  [Tut] Python | Split String into List of Substrings xSicKxBot 0 1,438 12-11-2022, 12:17 PM
Last Post: xSicKxBot
  [Tut] Python Find in List [Ultimate Guide] xSicKxBot 0 1,428 12-09-2022, 11:35 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016