Skip to main content

Working with Strings

f-strings and String Formatting

0:00
LearnStep 1/3

Mastering Python f-strings

Introduction to f-strings

Introduced in Python 3.6, f-strings (Formatted String Literals) provide a concise and readable way to embed expressions inside string literals. They are generally faster and more readable than the older % formatting or str.format().

Basic Usage

Prefix strings with f or F and write expressions inside curly braces {}.

python

Format Specifications

You can format values using a colon : followed by a format specifier.

python

Self-documenting Expressions (Debugging)

Python 3.8+ supports the = specifier for quick debugging. It prints the expression name, the equal sign, and the value.

python

Multiline f-strings

You can use triple quotes for multiline f-strings or combine them inside parentheses.

python