Learning Python becomes easier when you practice small, meaningful programs. This chapter covers the basic Python syntax practice questions, the print() function, comments, escape characters, and indentation through carefully selected beginner-friendly questions.
1. Python Program to Print “Hello, World!”
Difficulty: Foundation
Problem Statement
Write a Python program to print Hello, World! on the screen.
Expected Output
Hello, World!
Python Solution
print("Hello, World!")
Explanation
The print() function is used to display output on the screen.
2. Python Program to Print Multiple Lines
Difficulty: Foundation
Problem Statement
Write a Python program to display the following output using a single print() statement.
Expected Output
Name: John
Age: 20
City: New York
Python Solution
print("Name: John\nAge: 20\nCity: New York")
Explanation
The \n escape character moves the cursor to the next line.
3. Python Program to Display Formatted Text
Difficulty: Foundation
Problem Statement
Write a Python program to display the following output.
Expected Output
Python
Beginner
Practice
Python Solution
print("Python")
print("\tBeginner")
print("\t\tPractice")
Explanation
The \t escape character adds a tab space before the text.
4. Python Program to Use Comments
Difficulty: Foundation
Problem Statement
Write a Python program that prints Welcome to CodeMantra and add a comment to describe the code.
Expected Output
Welcome to CodeMantra
Python Solution
# This program prints a welcome message.
print("Welcome to CodeMantra")
Explanation
Comments begin with #. Python ignores comments during execution.
5. Python Program to Fix a Syntax Error
Difficulty: Practice
Problem Statement
The following code contains a syntax error. Fix it.
print("Welcome to CodeMantra'
Correct Solution
print("Welcome to CodeMantra")
Explanation
A string must start and end with the same quotation marks.
6. Python Program to Fix an Indentation Error
Difficulty: Practice
Problem Statement
The following code contains an indentation error. Rewrite it correctly.
if True:
print("Python")
Python Solution
if True:
print("Python")
Explanation
Python uses indentation to define blocks of code. The statement inside the if block must be indented.
7. Python Program to Print a Star Pattern
Difficulty: Practice
Problem Statement
Write a Python program to print the following pattern.
Expected Output
*
**
***
****
*****
Python Solution
print("*")
print("**")
print("***")
print("****")
print("*****")
Explanation
Each print() statement displays one line of the pattern.
8. Python Program to Create a Welcome Screen
Difficulty: Practice
Problem Statement
Write a Python program to display the following welcome screen.
Expected Output
=========================
CodeMantra
Learn Python Step by Step
=========================
Python Solution
print("=========================")
print(" CodeMantra")
print(" Learn Python Step by Step")
print("=========================")
Explanation
You can use spaces and symbols to create simple console layouts.
9. Python Program to Display Student Information
Difficulty: Challenge
Problem Statement
Write a Python program to display the following information.
Expected Output
Student Information
Name : Alex
Course : Python Programming
Age : 21
Python Solution
print("Student Information\n")
print("Name : Alex")
print("Course : Python Programming")
print("Age : 21")
Explanation
Use \n to create a blank line and format the output neatly.
10. Python Program to Create a Console Greeting Banner
Difficulty: Challenge
Problem Statement
Write a Python program to display the following output.
Expected Output
********************************
Welcome to CodeMantra Python
Happy Coding!
********************************
Python Solution
print("********************************")
print(" Welcome to CodeMantra Python")
print(" Happy Coding!")
print("********************************")
Explanation
This program combines multiple print() statements to create a simple console banner.
Key Concepts Covered
- Python syntax
print()function- Escape characters (
\n,\t) - Comments
- Indentation
- Syntax errors
- Output formatting
Frequently Asked Questions
What is the print() function in Python?
The print() function displays text, numbers, or variables on the screen.
What is indentation in Python?
Indentation means adding spaces before a line of code. Python uses indentation to define code blocks.
What is a comment in Python?
A comment starts with # and is used to explain code. Comments are ignored by Python.
What are escape characters?
Escape characters are special symbols such as \n (new line) and \t (tab space) used for formatting text.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
