Test Your Python Print() Function Knowledge

18 practice questions & exercises to test your proficiency and understanding of Python’s print() functionality. Whether you’re a beginner or an experienced programmer, it’s important to test your knowledge and skills regularly to ensure you’re staying up-to-date with the latest developments and best practices. In this article, we’ll provide a set of 18 questions that will help you gauge your proficiency in the Python print() function and identify areas where you can improve. So, let’s get started!

Practice Questions

Below are the lists of questions that you should solve to check your understanding of print() function. You can check the article about how to use print function before solving these practise questions. The questions have been categorized as basic, intermediate, and Advanced. Solutions are given at the end of the page. It’s always advised to try to solve it yourself and then if needed look at the solutions. Feel free to comment or ask queries.

Basic Questions

1: Write a Python program that prints the string “Hello, World!” to the console.

# INPUT:
Not required

# OUTPUT:
Hello, World!

2: Write a Python program that takes two integers as input and prints their sum to the console.

# INPUT:
5
7

# OUTPUT:
12

3: Write a Python program that prints a list of numbers from 1 to 10, separated by commas.

# INPUT:
no input

# OUTPUT:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

4: Write a Python program that prints a multiplication table for a given number. For example, if the input is 3, the output should be:

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
3 x 10 = 30

5: Write a Python program that prints the following text to the console, using only one print() statement:

*****
  ***
   *

Intermediate Questions:

6: Write a Python program that takes a list of strings as input and prints them to the console, each on a separate line, using the sep and end arguments to customize the output. Sample Input/Output:

#Input: 
["apple", "banana", "orange"]

#output:
apple
banana
orange

7: Write a Python program that takes a list of integers as input and prints them to the console, separated by commas, and with a custom message before and after the list, using the sep and end arguments. Sample Input/Output below:

#INPUT:
[1, 2, 3, 4, 5]

#OUTPUT:
The numbers are: 1, 2, 3, 4, 5.

8: Write a Python program that takes a list of strings as input, and prints the strings to the console, separated by a forward slash (/), and each string enclosed in double quotes.

#INPUT:
["apple", "banana", "cherry"]

#OUTPUT:
"apple"/"banana"/"cherry"

9: Write a Python program that prints the following text to the console, using only one print() statement and separating each word by a semicolon (;), but the last word should end with a colon (:).

#INPUT:
No input required

#OUTPUT:
apple;banana;cherry:

Advanced Questions

10: Write a Python program that prints the following text to a file named “output.txt”:

Hello, World!
This is a test file.

#OUTPUT:
The text should be printed to a file named "output.txt".

11: Write a Python program that takes a list of integers as input, and prints the integers to a file named “output.txt”, separated by commas (,) and spaces.

#Sample input: 
[1, 2, 3, 4, 5]

#Sample output: 
The integers should be printed to a file named "output.txt" in the following format: 1, 2, 3, 4, 5

12: Write a Python program that takes a dictionary as input, and prints the key-value pairs to a file named “output.txt” in the following format:

key1: value1
key2: value2
key3: value3

#Sample input: 
{"key1": "value1", "key2": "value2", "key3": "value3"}

#Sample output: 
The key-value pairs should be printed to a file named "output.txt" in the above format.

13: Write a Python program that reads a file named “input.txt”, replaces all occurrences of the word “apple” with “orange”, and prints the modified text to a file named “output.txt”.

Sample input (in “input.txt” file):

I like apple pie.
Do you like apples?

Sample output (in “output.txt” file):

I like orange pie.
Do you like oranges?

14: Write a Python program that prints the numbers 1 through 10 to the console, one number per line, and flushes the output after each number is printed.

#Sample input: 
No input required

#Sample output: 
The numbers 1 through 10 should be printed to the console, one number per line, with no delay between the numbers.

15: Write a Python program that takes a list of strings as input, and prints each string to the console with a delay of 1 second between each string. The program should flush the output after each string is printed.

#Sample input: 
["apple", "banana", "cherry"]

#Sample output: 
The strings "apple", "banana", and "cherry" should be printed to the console with a delay of 1 second between each string. The output should be immediately flushed after each string is printed.

16: Write a Python program that simulates the progress of a task by printing a series of dots to the console, one dot per second, until the task is complete. The program should flush the output after each dot is printed.

#Sample input: 
No Input required

#Sample output: 
The dots should be printed to the console, one dot per second, until the task is complete.

17: Write a Python program that takes a list of integers as input, and prints the running total of the integers to the console, with a delay of 1 second between each total. The program should flush the output after each total is printed.

#Sample input: 
[1, 2, 3, 4, 5]

#Sample output: 
The running total of the integers should be printed to the console, with a delay of 1 second between each total. The output should be immediately flushed after each total is printed.

18: Write a Python program that takes a list of numbers as input and prints them to the console, with the even numbers in blue and the odd numbers in red, using ANSI escape codes and the flush argument to ensure the colors are applied correctly. Hint: Ansi Escape Codes

Solutions

1: Solution Question 1

    print("Hello, world!")
    

    2: Solution Question 2

    #take 2 integers as input
    integer1 = int(input("enter first integer: "))
    integer2 = int(input("Enter second number: "))
    
    #output the sum of above integers and output to console
    print(integer1 + integer2)

    3: Solution Question 3

    print(*range(1,11), sep=",")

    4: Solution Question 4

    number = int(input())
    
    for num in range(1,11):
        print(f'{number} x {num} = {number * num}')

    5: Solution Question 5

    print("*****\n  ***\n   *")

    6: Solution Question 6

    input_string = ["apple", "banana", "orange"]
    
    print(*input_string, sep="\n")
    

    7: Solution Question 7

    input_string = [1, 2, 3, 4, 5]
    
    print("The numbers are:", end=" ")
    print(*input_string, sep=", ", end=".")

    8: Solution Question 8

    # Define the input list of integers
    my_list = ["apple", "banana", "cherry"]
    
    # Print the list with a custom message using the sep and end arguments
    print('"',end='')
    print(*my_list, sep='"/"', end='"')
    

    9: Solution Question 9

    print("apple", "banana", "cherry", sep=";", end=":")
    

    10: Solution Question 10

    #using print to write to file
    
    with open("output.txt", "w", encoding="utf-8") as new_file:
        print("Hello, World!\nThis is a test file.", file=new_file)

    11: Solution Question 11

    # Define the input list of integers
    numbers = [1, 2, 3, 4, 5]
    
    #output the result in output.txt
    
    with open("output.txt", "w", encoding="utf-8") as new_file:
        print(*numbers, sep=", ", file=new_file)

    12: Solution Question 12

    my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
    with open("output.txt", "w") as f:
        for key, value in my_dict.items():
            print(f"{key}: {value}", file=f)
    

    13: Solution Question 13

    with open ("input.txt", "r", encoding="utf-8") as input_file, open("output.txt", "w", encoding="utf-8") as output_file:
        for line in input_file:
            new_line = line.replace("apple", "orange")
            print(new_line, end="", file=output_file)
        

    14: Solution Question 14

    import time
    
    for num in range(1,11):
        print(num,end=" ",flush=True)
        time.sleep(1)
    

    15: Solution Question 15

    import time
    
    my_list = ["apple", "banana", "cherry"]
    for item in my_list:
        print(item, flush=True)
        time.sleep(1)
    

    16: Solution Question 16

    import time
    
    for i in range(10):
        print(".", end="", flush=True)
        time.sleep(1)
    print("Task complete.")
    

    17: Solution Question 17

    import time
    
    my_list = [1, 2, 3, 4, 5]
    total = 0
    for i in my_list:
        total += i
        print(total, flush=True)
        time.sleep(1)
    

    18: Solution Question 18

    import sys
    
    # Define the input list of numbers
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    # Use a loop to print each number in the list with the appropriate color
    for number in numbers:
        if number % 2 == 0:
            # Print even numbers in blue
            print(f"\033[94m{number}\033[0m ", end=" ")
        else:
            # Print odd numbers in red
            print(f"\033[91m{number}\033[0m ", end=" ")

    Congratulations on solving those programming questions! By taking the time to work through those problems, you’ve demonstrated a strong commitment to improving your coding skills. Not only have you gained a deeper understanding of the concepts involved, but you’ve also strengthened your problem-solving skills, which will serve you well in your future programming endeavors. Imagine the possibilities that await you now that you have these new skills under your belt! You could build your own software applications, contribute to open-source projects, or even land your dream job in the tech industry. So why stop now? Keep challenging yourself with more programming questions, and watch as your skills and achievements continue to grow. The sky’s the limit!

    Leave a Comment

    Share this