30 beginner-level print() exercises with their questions and solutions

🟢 Basic Printing (Q1–Q5)

Q1. Print your name using print().

print("Gaurav")

Q2. Print a message on two lines using \n.

print("Hello\nWorld")

Q3. Print the sentence: She said, "Python is fun!"

print('She said, "Python is fun!"')

Q4. Print a multi-line quote using triple quotes.

print("""Success is not final,
Failure is not fatal.""")

Q5. Print a backslash (\) and a tab (\t) in the same line.

print("This is a backslash: \\ and a tab:\tTabbed")

🟢 Printing with Variables (Q6–Q10)

Q6. Store your name in a variable and print it.

name = "Gaurav"
print(name)

Q7. Store two numbers in variables and print their sum.

a = 10
b = 20
print(a + b)

Q8. Print: Alice is 23 years old. using variables.

name = "Alice"
age = 23
print(name, "is", age, "years old.")

Q9. Use string concatenation to print "HelloWorld".

print("Hello" + "World")

Q10. Use , in print() to display multiple values.

print("Hello", "World", 123)

🟡 Formatted Printing (Q11–Q15)

Q11. Use f-string to print: Your score is 89 out of 100.

score = 89
print(f"Your score is {score} out of 100")

Q12. Use .format() to print: Hello, John! You have $50.

print("Hello, {}! You have ${}.".format("John", 50))

Q13. Print a float number with 2 decimal places.

pi = 3.14159
print(f"{pi:.2f}")

Q14. Print a percentage like: Result: 82.35%

percent = 82.3456
print(f"Result: {percent:.2f}%")

Q15. Align text left, right, and center in 10-character width.

text = "Hi"
print(f"{text:<10}")   # Left
print(f"{text:>10}")   # Right
print(f"{text:^10}")   # Center

🟣 Loops and Patterns (Q16–Q20)

Q16. Print numbers 1 to 5 using a loop.

for i in range(1, 6):
    print(i)

Q17. Print square of numbers from 1 to 3.

for i in range(1, 4):
    print(i**2)

Q18. Print a triangle pattern.

for i in range(1, 4):
    print("*" * i)

Q19. Print table of 5.

for i in range(1, 11):
    print(f"5 x {i} = {5*i}")

Q20. Print even numbers from 2 to 10.

for i in range(2, 11, 2):
    print(i)

🔵 Advanced Print Behaviors (Q21–Q25)

Q21. Use end=" " to print on the same line.

for i in range(5):
    print(i, end=" ")

Q22. Use sep="-" to print: 1-2-3-4

print(1, 2, 3, 4, sep="-")

Q23. Print a list with its type.

data = [1, 2, 3]
print(data, type(data))

Q24. Use print() inside a function.

def greet():
    print("Hello from function!")

greet()

Q25. Use escape characters to print a file path.

print("Path: C:\\new_folder\\python")

🟤 User Input & Display (Q26–Q30)

Q26. Ask for name using input() and greet them.

name = input("Enter your name: ")
print("Hello,", name)

Q27. Ask for two numbers and print their sum.

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)

Q28. Print an emoji using Unicode.

print("Smile 😀")

Q29. Print a simple menu.

print("1. Start")
print("2. Help")
print("3. Exit")

Q30. Print a progress bar using repeated characters.

progress = 5
print("[" + "#" * progress + " " * (10 - progress) + "] 50%")


Post a Comment

Previous Post Next Post