String Formatting with Placeholder Replacement

  • Share this:

Code introduction


This function accepts two string arguments, the first is the string to be formatted, and the second is the format string containing placeholders. The function returns the formatted string by replacing the placeholders in the format string with the string to be formatted.


Technology Stack : String

Code Type : String formatting

Code Difficulty : Intermediate


                
                    
def format_string(input_string, format_string):
    """
    Format a string using a given format string.

    Parameters:
    input_string (str): The string to be formatted.
    format_string (str): The format string containing placeholders.

    Returns:
    str: The formatted string.
    """
    return input_string.format(**dict(zip(format_string.split('{}'), [input_string] * len(format_string.split('{}')))))                
              
Tags: