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.7print(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
How to Pass a String into Subprocess.Popen (Using the Stdin Argument)
Manually Raising (Throwing) an Exception in Python
Why Can't Python'S Raw String Literals End With a Single Backslash
How to Count the Frequency of the Elements in an Unordered List
Process Escape Sequences in a String in Python
Django Csrf Check Failing With an Ajax Post Request
Taking Subarrays from Numpy Array With Given Stride/Stepsize
Why Does Substring Slicing With Index Out of Range Work
How to Find the Location of My Python Site-Packages Directory
What Is Getattr() Exactly and How to Use It
Permutations With Unique Values
Save Plot to Image File Instead of Displaying It Using Matplotlib
How to Replace Nan Values by Zeroes in a Column of a Pandas Dataframe
How to Get the Source Code of a Python Function
Efficient Way to Rotate a List in Python