Basic print syntax

Code Examples

Example 1

Basic print statement.

print("Hello, World!")
# Outputs: Hello, World!

Example 2

Printing numbers.

print(42)
# Output: 42

Example 3

Printing multiple items (separated by spaces).

print("The answer is", 42)
# Output: The answer is 42

Example 4

Using string concatenation with +.

print("Hello" + " " + "World!")
# Output: Hello World!

Example 5

Printing mathematical expressions.

print(5 + 3)
# Output: 8

print(10 * 2)
# Output: 20

print(20 / 4)
# Output: 5.0

Example 6

Printing with quotes inside strings. The backslash (\) in Python's print() function is called an escape character. It tells Python that the character immediately following it should be treated specially, rather than as a literal character. In this example, the backslashes before the double quotes (\") are telling Python that these quotes are part of the text being printed, not markers for the beginning or end of the string itself.

print("She said, \"Python is fun!\"")
# Output: She said, "Python is fun!"

Example 7

Printing escape characters. When you include \n in a string, it creates a line break at that position when the string is printed.

print("First line\nSecond line")
# Output:
# First line
# Second line

Example 8

When you include \t in a string, it creates a horizontal spacing at that position when the string is printed.

print("Tab\tspacing")
# Output: Tab     spacing

Example 9

Printing empty lines.

print()
# Output: 
# (blank line)

Example 10

Printing boolean values.

print(True)
# Output: True

print(False)
# Output: False