Python String Farmatting


In Python, there are several ways to format strings. Here are the most commonly used methods:

1. %-formatting

This is the oldest method, using the % operator to interpolate variables into a string.

name = "Alice"
age = 30
formatted_string = "My name is %s and I am %d years old." % (name, age)
print(formatted_string)


2. str.format()

Introduced in Python 2.6, this method uses curly braces {} as placeholders within the string,

which are replaced by the values passed to the format() method.

name = "Alice"
age = 30
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)

You can also specify the order of arguments:

name = "Alice"
age = 30
formatted_string = "My name is {0} and I am {1} years old.".format(name, age)
print(formatted_string)

Named placeholders are also possible:

name = "Alice"
age = 30
formatted_string = "My name is {name} and I am {age} years old.".format(name=name, age=age)
print(formatted_string)

Named placeholders are also possible:

name = "Alice"
age = 30
formatted_string = "My name is {name} and I am {age} years old.".format(name=name, age=age)
print(formatted_string)


3. f-Strings (Formatted String Literals)

Introduced in Python 3.6, f-strings are prefixed with f and allow for inline expressions to be evaluated.

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

You can also use expressions inside the curly braces:

name = "Alice"
age = 30
formatted_string = f"In 5 years, {name} will be {age + 5} years old."
print(formatted_string)


4. Template Strings

The string module's Template class provides another way for string formatting.

This is more secure for handling user inputs, as it uses $ for placeholders and avoids arbitrary code execution.

from string import Template

name = "Alice"
age = 30
template = Template("My name is $name and I am $age years old.")
formatted_string = template.substitute(name=name, age=age)
print(formatted_string)


Comparison and Use Cases

%-formatting:

  • Pros: Simple and straightforward.
  • Cons: Less readable with multiple variables, not as flexible as newer methods.
  • Use Case: Legacy codebases where backwards compatibility is necessary.

str.format():

  • Pros: More powerful and flexible than %-formatting. Supports named placeholders and reordering.
  • Cons: Verbose compared to f-strings.
  • Use Case: When you need more control over formatting, especially with complex formatting and named placeholders.

f-Strings:

  • Pros: Concise, readable, and supports inline expressions.
  • Cons: Requires Python 3.6 or later.
  • Use Case: General use for most string formatting tasks, especially when you want a clean and readable syntax.

f-Strings:


  • Pros: Concise, readable, and supports inline expressions.
  • Cons: Requires Python 3.6 or later.
  • Use Case: General use for most string formatting tasks, especially when you want a clean and readable syntax.

Template Strings:

  • Pros: Safe for handling user inputs as they prevent code injection.
  • Cons: Less powerful and flexible compared to str.format() and f-strings.
  • Use Case: When dealing with user-generated strings, to ensure security.

Each method has its strengths and ideal use cases, and choosing the right one depends on the specific requirements of your code.