Underline Text Function - Python Programming Exercise

In this exercise, you will develop a Python function that can center the text on the screen (assuming a screen width of 80 characters) and then underline it with hyphens. This exercise is perfect for practicing string manipulation and formatting in Python. By implementing this function, you will gain hands-on experience in handling string manipulation and formatting in Python. This exercise not only reinforces your understanding of string manipulation but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Underline Text Function

 Objective

Develop a Python function that can center the text on the screen (assuming a screen width of 80 characters) and then underline it with hyphens:

write_underlined("Hello!")

 Example Python Exercise

 Copy Python Code
# Define the function write_underlined which accepts text as a parameter
def write_underlined(text):
    screen_width = 80  # Assume the width of the screen is 80 characters
    # Calculate the padding needed on the left side to center the text
    padding_left = (screen_width - len(text)) // 2  # Calculate the number of spaces to center the text
    # Print the centered text
    print(' ' * padding_left + text)  # Print the text with leading spaces for centering
    # Print the underline below the text. Only underline the letters, not the spaces.
    underline = '-' * len(text)  # Create the underline with the same length as the text
    print(' ' * padding_left + underline)  # Print the underline with the same length as the text

# Main code to call write_underlined with the text "Hello!"
write_underlined("Hello!")  # Call the function with the text "Hello!"

 Output

                                   Hello!
                                   ------

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Array Summation Function

    In this exercise, you will develop a Python program to calculate the sum of the elements in an array. The main function should look like this: def main(): example = [...

  •  Double Value Function

    In this exercise, you will develop a Python function named "double" to calculate and return an integer that is twice its input. This exercise is perfect for pr...

  •  Function Double with Mutable Parameter

    In this exercise, you will develop a Python function named "double_value" to calculate the double of an integer number and modify the data passed as an argument. Sinc...

  •  Function Swap with Mutable Parameters

    In this exercise, you will develop a Python program with a function called "swap" to exchange the values of two integer variables, which are passed by reference. This...

  •  Optimizing Local Variables in Functions

    In this exercise, you will develop a Python program with a function named "power" to compute the result of raising one integer to the power of another positive intege...

  •  Recursive Function for Calculating Powe

    In this exercise, you will develop a Python program with a function that computes the result of raising one integer to the power of another integer (e.g., 5 raised to...