Different Ways to Use Python's Print Statement - Q & A
Q 1: How do you print a simple string in Python?
Ans:
print("Hello, Python!") # Output: Hello, Python!
Q 2: How to print a number in Python?
Ans:
print(123) # Output: 123
Q 3: How to print a floating-point number with 2 decimal places?
Ans:
print("%.2f" % 7.34567) # Output: 7.35
Q 4: How to concatenate two strings in a print statement?
Ans:
print("Hello" + " Python") # Output: Hello Python
Q 5: How to print multiple variables in a single statement?
Ans:
x = "Learn Python on"
y = "Tech Skill Guru"
print(x, y) # Output: Learn Python on Tech Skill Guru
Q 6: How to print a string with a variable using f-string formatting?
Ans:
name = "Alice"
print(f"My name is {name}") # Output: My name is Alice
Q 7: How to print a multiline string in Python?
Ans:
print("""This is a
multiline
string""")
# Output: This is a
# multiline
# string
Q 8: How to print a string with a newline character?
Ans:
print("Hello
Python")
# Output: Hello
# Python
Q 9: How to print a string with a tab character?
Ans:
print("Hello Python") # Output: Hello Python
Q 10: How to print the result of a mathematical operation?
Ans:
print(5 * 4) # Output: 20
Q 11: How to print a string with both single and double quotes?
Ans:
print('She said, "Hello, Python!"') # Output: She said, "Hello, Python!"
Q 12: How to print a formatted price of a product: "$100.00"?
Ans:
price = 100
print(f"${price:.2f}") # Output: $100.00
Q 13: How to print the type of a variable?
Ans:
var = "Python"
print(type(var)) # Output: <class 'str'>
Q 14: How to use format( ) in print statement?
Ans: You can use {}
as placeholders where you want to insert values.
print("Hello, {}. You are {} years old.".format("Alice", 25))
# Output: Hello, Alice. You are 25 years old.
You can refer to values by their positional index in the format method.
print("{0} and {1}".format('spam', 'eggs'))
# Output: spam and eggs
print("{1} and {0}".format('spam', 'eggs'))
# Output: eggs and spam
print("Coordinates: {latitude}, {longitude}".format(latitude='37.24N', longitude='-115.81W'))
# Output: Coordinates: 37.24N, -115.81W
Q 15: How to use sep and end in print statement?
Ans:
Sep: This parameter defines the character or sequence of characters used to separate multiple arguments passed to the print()
function. By default, this is a single space character ' '
.But you can change the separator using sep
. For example, to use a hyphen as a separator:
print("Hello", "World", sep="-") # Output: Hello-World
End : This parameter defines what character or sequence of characters is printed at the end of the output. By default, this is a newline character
, which means that after a call to print()
, anything else printed will start on a new line.
print("Hello")
print("World")
# Output: Hello
# World
But you can change the ending character using end
. For example, to end with a space:
print("Hello", end=" ")
print("World")
# Output: Hello World
Exercise for Practice
-
Print a mathematical expression that adds 10 and 20.
-
Use a print statement to display a string and a number together. For example, "I have 5 apples".
-
Use the print statement to display two different strings in one line, like "Hello" and "Python".
-
Use the
sep
parameter in the print statement to print numbers 1, 2, and 3 separated by a comma. -
Write a Python program to print a floating point number (like 12.3456) with two decimal places.
-
Use the print statement to display a multiline string in Python.
-
Use the
end
parameter in the print statement to print "Python" and "Programming" on the same line with a space in between. -
Write a Python program to print a string with a variable using f-string formatting. For example, "My name is [Your_Name]".
-
Print the result of a mathematical operation, like the multiplication of 10 and 5, using the print statement.
-
Write a Python program to print the type of a variable.
-
Write a Python program to print a string with newline and tab characters.
-
Use a print statement to display a string with both single and double quotes.