String Formatting Function Overview

  • Share this:

Code introduction


This function takes an input string and a format type, then returns the string formatted according to the specified type, such as uppercase, lowercase, capitalizing the first letter, or title case. If an unsupported format type is provided, it raises a ValueError.


Technology Stack : String manipulation

Code Type : String formatting

Code Difficulty : Intermediate


                
                    
def format_string(input_str, format_type='upper'):
    if format_type == 'upper':
        return input_str.upper()
    elif format_type == 'lower':
        return input_str.lower()
    elif format_type == 'capitalize':
        return input_str.capitalize()
    elif format_type == 'title':
        return input_str.title()
    else:
        raise ValueError("Unsupported format type")