Part of the series

Several example codes

~1 min read • Updated Oct 11, 2025

Program Overview

This Python program reads a number from the user to determine how many lines to print.
In each line, it displays a sequence of 'x' characters, one for each character in a sample text.
This exercise helps practice nested loops and character-level output formatting in the terminal.


Python Code:


def display_text_with_x(rows, text="Hello"):
    for i in range(rows):
        for char in text:
            print("x", end="")
        print()

# Run the program
rows = int(input("Enter number of lines: "))
display_text_with_x(rows)

Sample Output (input: 3, text: "Hello"):


xxxxx
xxxxx
xxxxx

Step-by-Step Explanation:

- The program reads the number of lines from the user
- A nested loop is used: the outer loop controls the number of lines
- The inner loop iterates over each character in the sample text
- Instead of printing the actual character, it prints 'x' for each one
- The result is a grid of 'x' characters, matching the length of the text


Written & researched by Dr. Shahin Siami