Introduction π
Output functions allow your Python program to show messages, results, and formatted text on the screen. The primary output function in Python is print(), and it is one of the most used functions in any Python program.
Note
1. The print() Function β Basics π§©
The print() function displays data on the console. You can print text, numbers, variables, or even formatted messages.
print_basic.py
print("Hello, Python!")print_variables.py
name = "Sathish"
age = 25
print(name, age)Note
2. Printing Multiple Values π¦
You can print several values by separating them with commas.
multiple_values.py
print("Name:", "Sathish", "Age:", 25)Note
3. Using sep and end Parameters βοΈ
sep β Custom Separator
Controls how multiple values are separated.
sep_example.py
print("2025", "12", "08", sep="-")end β Custom Ending
By default, print() ends with a newline (\n). You can change this with the end parameter.
end_example.py
print("Hello", end=" ")
print("World")Note
4. Formatting Output π¨
You can format output using three common methods:
1. f-Strings (Recommended)
fstring.py
name = "Sathish"
age = 25
print(f"My name is {name} and I am {age} years old.")2. format() Method
format_method.py
print("My name is {} and I am {} years old.".format("Sathish", 25))3. % Formatting (Older Style)
percent_format.py
print("My name is %s and I am %d years old." % ("Sathish", 25))Note
5. Escape Characters π§
Use escape sequences to print special characters.
| Escape Code | Description | Example |
|---|---|---|
| \n | New Line | print("Hello\nWorld") |
| \t | Tab Space | print("A\tB") |
| \' | Single Quote | print('It\'s Python') |
| \" | Double Quote | print("He said \"Hi\"") |
escape_example.py
print("Hello\nWorld")
print("A\tB")6. Printing Without Newline π
Use end="" to print without automatically moving to the next line.
same_line.py
for i in range(5):
print(i, end=" ")7. Printing Raw Strings π
Use r"" to print backslashes without escape behavior.
raw_string.py
print(r"C:\Users\Sathish")8. output With File Writing (Optional) π
print() can also write to files using the file parameter.
file_output.py
with open("output.txt", "w") as f:
print("Hello File!", file=f)Note
9. Real-World Output Example π―
real_world_example.py
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}! Next year, you will be {age + 1} years old.")Conclusion π
You now understand all major ways of displaying output in Python. Want a tutorial on operators, expressions, conditional statements, loops, or functions? Just tell me! π