String Formatting Function

  • Share this:

Code introduction


This function formats the input string based on the specified format type, such as stripping whitespace, left alignment, right alignment, and center alignment.


Technology Stack : String processing

Code Type : String processing

Code Difficulty : Intermediate


                
                    
def string_formatting(text, format_type="s"):
    if format_type == "s":
        return text.strip()
    elif format_type == "l":
        return text.ljust(20)
    elif format_type == "r":
        return text.rjust(20)
    elif format_type == "c":
        return text.center(20)
    else:
        return "Invalid format type"