Function to Print Title - Python Programming Exercise

In this exercise, you will develop a Python program with a function called 'WriteTitle' that displays a text in uppercase, centered on the screen with extra spaces and lines above and below it. This exercise is perfect for practicing function definition, string manipulation, and formatting in Python. By implementing this function, you will gain hands-on experience in handling function definitions, string manipulation, and formatting in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Print Title

 Objective

Develop a Python program with a function called 'WriteTitle' that displays a text in uppercase, centered on the screen with extra spaces and lines above and below it. For example, calling WriteTitle('Welcome!') will produce an output like this, centered in an 80-column width:

--------------- W E L C O M E ! ---------------

(The number of hyphens should automatically adjust based on the text length.)

 Example Python Exercise

 Copy Python Code
# Function to display a title in uppercase, centered with extra spaces and lines above and below it
def WriteTitle(text):
    # Convert the text to uppercase
    text = text.upper()
    
    # Calculate the total width of the screen (80 characters)
    screen_width = 80
    
    # Calculate how many spaces are needed on the left and right to center the text
    spaces = (screen_width - len(text) - 2) // 2  # The extra 2 accounts for the space on both sides of the text
    
    # Create a line of hyphens based on the text length
    line = '-' * (len(text) + 2)  # The 2 extra characters account for spaces around the text
    
    # Print a line of hyphens above the text
    print(line)
    
    # Print the text centered with spaces on both sides
    print(' ' * spaces + text + ' ' * spaces)
    
    # Print a line of hyphens below the text
    print(line)

# Main function to demonstrate the WriteTitle function
def main():
    # Example title to display
    title = "Welcome!"
    
    # Call the WriteTitle function to display the title
    WriteTitle(title)

# Run the main function
if __name__ == "__main__":
    main()

 Output

--------------- WELCOME! ---------------

 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.

  •  Main Function Output Value

    In this exercise, you will develop a Python program that prompts the user to provide a title via the command line (using the previously defined WriteTitle function). ...

  •  Function to Calculate DV

    In this exercise, you will develop a Python program that defines a function to count the number of numeric digits and vowels in a given string. The function should ta...

  •  Function to Verify Alphabetic Characters

    In this exercise, you will develop a Python program that includes a function to determine if a given character is alphabetic (from A to Z). This exercise is pe...

  •  Function to Check Numeric Value

    In this exercise, you will develop a Python program that defines a function to check if a string represents an integer value. This exercise is perfect for prac...

  •  Main Function Parameters for Calculator

    In this exercise, you will develop a Python program that performs basic arithmetic operations such as addition, subtraction, multiplication, or division based on comm...

  •  Main Function Parameters and Return Value for Calculator

    In this exercise, you will develop a Python program that performs arithmetic operations such as addition, subtraction, multiplication, or division by analyzing comman...