String Formatting

f-strings (Formatted String Literals) – Python 3.6+

f"some text {expression} more text"
  • Variables or expressions inside {} are evaluated at runtime.

  • Faster and more readable than other methods.

Example

Output:

Advanced formatting with f-strings

str.format() Method – Python 2.7+ / 3.x

Placeholder
Description

{}

Automatic

{0}, {1}

Positional

{name}

Keyword

Example

Padding and alignment

Last updated