String Formatting with Custom Placeholders

  • Share this:

Code introduction


This function accepts a string and a new format string, then reformats the input string using the new format string. The new format string can contain placeholders, such as {0}, {1}, etc., which will be replaced by the corresponding parts of the input string.


Technology Stack : String

Code Type : String formatting

Code Difficulty : Intermediate


                
                    
def format_string(input_str, new_format="{0} {1} {2}"):
    parts = input_str.split()
    formatted_str = new_format.format(parts[0], parts[1], parts[2:])
    return formatted_str                
              
Tags: