Python str.format() function


In Python, the str.format() method is used to format strings by embedding values within a string using placeholders. This method provides a powerful way to create formatted output and is widely used for constructing strings in a readable and maintainable way.

Syntax

str.format(*args, **kwargs)
  • *args: Positional arguments that can be passed to the placeholders in the string.
  • **kwargs: Keyword arguments that can also be used in the placeholders.

Placeholders

Placeholders in the string are defined using curly braces {}. You can use various options within the braces to specify formatting options, such as number formatting, alignment, width, and more.

Example Usage

  1. Basic formatting with positional arguments:
name = "Alice" age = 30 formatted_string = "My name is {} and I am {} years old.".format(name, age) print(formatted_string) # Output: "My name is Alice and I am 30 years old."
  1. Using indexed placeholders:

You can use indexed placeholders to refer to specific arguments:

formatted_string = "My name is {0} and I am {1} years old. {0} is a programmer.".format(name, age) print(formatted_string) # Output: "My name is Alice and I am 30 years old. Alice is a programmer."
  1. Using keyword arguments:

You can also use named placeholders with keyword arguments:

formatted_string = "My name is {name} and I am {age} years old.".format(name=name, age=age) print(formatted_string) # Output: "My name is Alice and I am 30 years old."
  1. Formatting numbers:

You can format numbers, including decimal places and thousands separators:

pi = 3.141592653589793 formatted_string = "Pi is approximately {:.2f}.".format(pi) print(formatted_string) # Output: "Pi is approximately 3.14."
  1. Alignment and width:

You can control the alignment and width of the formatted output:

name = "Bob" formatted_string = "|{:<10}|{:^10}|{:>10}|".format(name, name, name) print(formatted_string) # Output: "|Bob | Bob | Bob|"
  1. Using format specifiers:

You can use format specifiers for more advanced formatting:

number = 1234.56789 formatted_string = "Number with commas: {:,.2f}".format(number) print(formatted_string) # Output: "Number with commas: 1,234.57"

Summary

  • Use str.format() to format strings dynamically by embedding values within placeholders.
  • You can use both positional and keyword arguments for flexibility in formatting.
  • The method allows for various formatting options, including number formatting, alignment, and width control.
  • This method is a powerful tool for constructing readable and maintainable output strings in your applications.

Note on f-strings

Starting from Python 3.6, f-strings (formatted string literals) are also available and provide a more concise syntax for string formatting. For example:

name = "Alice" age = 30 formatted_string = f"My name is {name} and I am {age} years old." print(formatted_string) # Output: "My name is Alice and I am 30 years old."

F-strings are often preferred for their simplicity and readability, but str.format() remains a powerful and flexible option.