Python Print() Function: A Practical Guide with Examples
Master the print() function in Python—your essential tool for displaying messages, debugging, and building user interfaces. Whether you’re a beginner or a seasoned developer, this guide covers the basics and dives into advanced techniques, such as printing blank lines and customizing the line terminator.
What is print()?
The print() built‑in function outputs one or more objects to the console. Internally, it converts each object to a string, joins them with a space, and appends a newline unless you override the end parameter.
Syntax
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
For most use cases, the simple form print(object) is sufficient. See the official docs for a full list of parameters.
Printing a Simple String
In Python 3, a single string is printed as follows:
print("Welcome to Guru99")
Output:
Welcome to Guru99
Python 2 used a different syntax without parentheses:
print "Welcome to Guru99"
Multiple Lines
Print several strings, one per line:
print("USA")
print("Canada")
print("Germany")
print("France")
print("Japan")
Output:
USA Canada Germany France Japan
Printing Blank Lines
To insert blank lines, you can either multiply the newline character or write them explicitly.
print(8 * "\n") # eight empty lines
# or
print("\n\n\n\n\n\n\n\n")
Example that surrounds a greeting with eight blank lines:
print("Welcome to Guru99")
print(8 * "\n")
print("Welcome to Guru99")
Result:
Welcome to Guru99 Welcome to Guru99
Customizing the Line Terminator
By default, print() ends with a newline (\n). The end keyword lets you replace it with any string, which is handy for creating formatted output.
print("Welcome to", end=' ')
print("Guru99", end='!')
Output:
Welcome to Guru99!
print("Python", end='@')
Output:
Python@
Using end can also help build tables or progress indicators without breaking the flow of a single line.
Python
- Getting Started with Python: Install, Run, and Write Your First Program
- Master Python’s str.count(): How to Count Characters & Substrings with Examples
- Master Python `format()` Strings with Clear Examples
- Master Python's String.find() Method: Syntax, Examples & Alternatives
- Master Python Lambda Functions: Practical Examples & Best Practices
- Python round() Function Explained with Practical Examples
- Mastering Python's map() Function: Syntax, Examples, and Best Practices
- Python timeit() – Measuring Execution Time with Practical Examples
- Python list.count(): Expert Guide with Practical Examples
- Python Module Importing – A Practical Guide with Examples