print()
print()
to Display Output:
In Python, the print()
function is used to display output to the console. You can print strings, numbers, variables, and more. The print()
function automatically adds a new line after each call, but you can change this behavior using the "end" parameter.
# Printing a simple message
print("Hello, World!")
# Printing variables
name = "Alice"
age = 30
print("Name:", name, "Age:", age)
# Printing numbers
x = 10
y = 20
sum = x + y
print("Sum of", x, "and", y, "is", sum)
# Customizing the end parameter
print("Hello", end=" ")
print("World!")
You can format the output using string formatting methods such as f-strings or the format()
method. These methods allow you to embed variables and expressions within the printed text.
# Using f-strings
name = "Bob"
age = 25
print(f"My name is {name} and I am {age} years old.")
# Using the format() method
item = "book"
price = 19.99
print("The {} costs ${:.2f}".format(item, price))
print()
:
The print()
function can take multiple arguments separated by commas. It will print each argument with a space in between. You can use this feature to print multiple items on the same line.
# Printing multiple items on the same line
name = "Charlie"
age = 35
city = "New York"
print(name, age, city, "are friends.")
The print()
function is a versatile tool for displaying output and helps you see the results of your Python code.