Python Print Function Examples
Welcome to a beginner-friendly collection of practical print() function examples in Python. These hands-on snippets will help you understand how to display text, numbers, and formatted output—perfect for those just getting started with coding.
1. Write a program that prints "Hello, world!".
1. Write a program that prints "Hello, world!".
python
# Example 1: Print a simple message
print("Hello, world!")
# Example 1: Print a simple message
print("Hello, world!")
2. Write a program that prints the number 100.
2. Write a program that prints the number 100.
python
# Example 2: Print a number
print(100)
# Example 2: Print a number
print(100)
3. Write a program that prints your age.
3. Write a program that prints your age.
python
# Example 3: Print your age
age = 20 # Replace with your actual age
print(age)
# Example 3: Print your age
age = 20 # Replace with your actual age
print(age)
4. Write a program that prints a string with quotes around it.
4. Write a program that prints a string with quotes around it.
python
# Example 4: Print a string with quotes
print('"Hello, World!"')
# Example 4: Print a string with quotes
print('"Hello, World!"')
5. Write a program that prints multiple values, separated by a comma.
5. Write a program that prints multiple values, separated by a comma.
python
# Example 5: Print multiple values separated by a comma
first_name = "Alice"
last_name = "Smith"
age = 25
print(first_name, last_name, age, sep=',')
# Example 5: Print multiple values separated by a comma
first_name = "Alice"
last_name = "Smith"
age = 25
print(first_name, last_name, age, sep=',')
6. Write a program that prints multiple values separated by a dash (-).
6. Write a program that prints multiple values separated by a dash (-).
python
# Example 6: Print multiple values separated by a dash
first_name = "Alice"
last_name = "Smith"
age = 25
print(first_name, last_name, age, sep="-")
# Example 6: Print multiple values separated by a dash
first_name = "Alice"
last_name = "Smith"
age = 25
print(first_name, last_name, age, sep="-")
7. Write a program that prints a value without a newline at the end.
7. Write a program that prints a value without a newline at the end.
python
# Example 7: Print without a newline at the end
print("Hello", end="")
print(" World!")
# Example 7: Print without a newline at the end
print("Hello", end="")
print(" World!")
8. Write a program that prints to a file instead of the console.
8. Write a program that prints to a file instead of the console.
python
# Example 8: Print to a file
with open("output.txt", "w") as f:
print("This will be written to a file.", file=f)
# Example 8: Print to a file
with open("output.txt", "w") as f:
print("This will be written to a file.", file=f)
9. Write a program that prints a formatted string with variables using f-strings.
9. Write a program that prints a formatted string with variables using f-strings.
python
# Example 9: Print a formatted string with variables using f-strings
name = "John"
age = 30
print(f"Hello, my name is {name} and I am {age} years old.")
# Example 9: Print a formatted string with variables using f-strings
name = "John"
age = 30
print(f"Hello, my name is {name} and I am {age} years old.")
10. Write a program that prints formatted text with left, right, and center alignment.
10. Write a program that prints formatted text with left, right, and center alignment.
python
# Example 10: Print formatted text with alignment
name = "Alice"
print(f"{name:<10} is left aligned.")
print(f"{name:>10} is right aligned.")
print(f"{name:^10} is centered.")
# Example 10: Print formatted text with alignment
name = "Alice"
print(f"{name:<10} is left aligned.")
print(f"{name:>10} is right aligned.")
print(f"{name:^10} is centered.")
11. Write a program that prints a floating-point number with a specified decimal precision.
11. Write a program that prints a floating-point number with a specified decimal precision.
python
# Example 11: Print formatted numbers with decimal precision
pi = 3.14159
print(f"Value of Pi: {pi:.2f}")
# Example 11: Print formatted numbers with decimal precision
pi = 3.14159
print(f"Value of Pi: {pi:.2f}")
12. Write a program that uses newline and tab characters in the output.
12. Write a program that uses newline and tab characters in the output.
python
# Example 12: Print with newline and tab characters
print("Line 1\nLine 2")
print("Name:\tAlice")
# Example 12: Print with newline and tab characters
print("Line 1\nLine 2")
print("Name:\tAlice")
13. Write a program that prints multiple lines using triple quotes.
13. Write a program that prints multiple lines using triple quotes.
python
# Example 13: Print multiline string
print("""This is line 1
This is line 2
This is line 3""")
# Example 13: Print multiline string
print("""This is line 1
This is line 2
This is line 3""")
14. Write a program that prints an empty line.
14. Write a program that prints an empty line.
python
# Example 14: Print an empty line
print()
# Example 14: Print an empty line
print()
15. Write a program that prints a variable inside a string using the .format() method.
15. Write a program that prints a variable inside a string using the .format() method.
python
# Example 15: Print with .format() method
name = "Alice"
age = 25
print("Name: {}, Age: {}".format(name, age))
# Example 15: Print with .format() method
name = "Alice"
age = 25
print("Name: {}, Age: {}".format(name, age))
16. Write a program that prints a string containing an escape sequence.
16. Write a program that prints a string containing an escape sequence.
python
# Example 16: Print with escape sequence
print("This is a backslash: \\")
# Example 16: Print with escape sequence
print("This is a backslash: \\")
17. Write a program that prints Unicode characters like emojis or symbols.
17. Write a program that prints Unicode characters like emojis or symbols.
python
# Example 17: Print Unicode characters
print("I love Python \u2764\uFE0F")
# Example 17: Print Unicode characters
print("I love Python \u2764\uFE0F")
18. Write a program that prints a raw string to avoid interpreting escape characters.
18. Write a program that prints a raw string to avoid interpreting escape characters.
python
# Example 18: Print raw strings
print(r"C:\Users\Name\Documents")
# Example 18: Print raw strings
print(r"C:\Users\Name\Documents")
19. Write a program that prints a repeated pattern using string multiplication.
19. Write a program that prints a repeated pattern using string multiplication.
python
# Example 22: Print repeated pattern
print("=" * 20)
# Example 22: Print repeated pattern
print("=" * 20)
20. Write a program that prints the raw representation of a string using repr().
20. Write a program that prints the raw representation of a string using repr().
python
# Example 20: Print with repr()
text = "Hello\nWorld"
print(repr(text))
# Example 20: Print with repr()
text = "Hello\nWorld"
print(repr(text))
21. Write a program that prints immediately using flush=True.
21. Write a program that prints immediately using flush=True.
python
# Example 21: Print with flush
import time
print("Loading...", flush=True)
time.sleep(2)
print(" Done!")
# Example 21: Print with flush
import time
print("Loading...", flush=True)
time.sleep(2)
print(" Done!")
What's Next?
In the next tutorial, we’ll walk through practical examples that show how to use Python's built-in data types and how to apply arithmetic, comparison, and logical operators effectively.