Multiple Prints on the Same Line in Python

How to print multiple lines of text with Python

You can use triple quotes (single ' or double "):

a = """
text
text
text
"""

print(a)

Print new output on same line

From help(print):

Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.

You can use the end keyword:

>>> for i in range(1, 11):
... print(i, end='')
...
12345678910>>>

Note that you'll have to print() the final newline yourself. BTW, you won't get "12345678910" in Python 2 with the trailing comma, you'll get 1 2 3 4 5 6 7 8 9 10 instead.

Want to print multiple times in single line Python

In python 3 you can use flush=True:

for character in welcome:
print(character, end="", flush=True)
time.sleep(0.2)

I made the code clearer by replacing for i in range(len(welcome)) by for word in welcome, so you just have to print character.

Multiple prints in the same lines(cards)

Firstly I would drop explicit .format and replace with f in front of string with {} this way you can easily input stuff.

Secondly I would craft """ """ multiple line string instead of many " " this way you could craft string with two cards next to each other.

I can't comment so I will edit answer. Yes like you did with additional parameter

print(<card_string>, end="")

this way print won't input "\n" and next print will be next to the last one.
You just have to play with it a little.

Print in one line dynamically

Change print item to:

  • print item, in Python 2.7
  • print(item, end=" ") in Python 3

If you want to print the data dynamically use following syntax:

  • print(item, sep=' ', end='', flush=True) in Python 3


Related Topics



Leave a reply



Submit