String Formatting Function Overview

  • Share this:

Code introduction


This function formats the input string based on the specified format type, which can be title case, uppercase, lowercase, or capitalize the first letter.


Technology Stack : str.title(), str.upper(), str.lower(), str.capitalize()

Code Type : String formatting function

Code Difficulty : Intermediate


                
                    
def format_string(input_str, format_type='title'):
    if format_type == 'title':
        return input_str.title()
    elif format_type == 'upper':
        return input_str.upper()
    elif format_type == 'lower':
        return input_str.lower()
    elif format_type == 'capitalize':
        return input_str.capitalize()
    else:
        return input_str