Python F-Strings

Python F-Strings

Kishore V


Python F-Strings

F-strings (formatted string literals) were introduced in Python 3.6 and are now the recommended and most efficient way to format strings in Python.

Before Python 3.6, developers mainly used the format() method or string concatenation, which was more verbose and harder to read.

What Are F-Strings?

An f-string lets you embed expressions directly inside string literals.

To create an f-string, prefix the string with the letter f.

Example: Basic F-String

Output:

Welcome to Python programming
Try it Yourself

Using Placeholders in F-Strings

Values are inserted into an f-string using curly braces {}.

Inside these braces, you can place:

  • Variables
  • Calculations
  • Function calls
  • Formatting modifiers

Example: Insert a Variable

Output:

Your final score is 88
Try it Yourself

Formatting Values with Modifiers

You can control how values appear using formatting modifiers.

Modifiers are added after a colon : inside the placeholder.

Example: Limit Decimal Places

Output:

Average rating: 7.46
Try it Yourself

Example: Format a Value Directly

Output:

Success rate: 92.3%
Try it Yourself

Performing Operations in F-Strings

Python expressions can be evaluated directly inside placeholders.

Example: Arithmetic Operation

Output:

Total cost: 1800 rupees
Try it Yourself

Example: Calculation Using Variables

Output:

Payable amount: 450.0
Try it Yourself

Conditional Logic Inside F-Strings

You can use conditional expressions (if...else) within placeholders.

Example: Conditional Output

Output:

Result: Pass
Try it Yourself

Calling Functions in F-Strings

Both built-in and user-defined functions can be executed inside f-strings.

Example: Built-in Function

Output:

I live in Chennai
Try it Yourself

Example: User-Defined Function

Output:

Distance: 6.21 miles
Try it Yourself

Advanced Formatting Options

Example: Thousand Separator

Output:

Population: 14,500,000
Try it Yourself

Example: Percentage Format

Output:

Completed: 88%
Try it Yourself

Common Formatting Types

Type Description
<Left align
>Right align
^Center align
+Show sign
,Thousand separator
_Underscore separator
bBinary
dDecimal
fFixed-point
eScientific
oOctal
x / XHexadecimal
%Percentage

String Formatting Using format()

Before f-strings, the format() method was commonly used.

Although still supported, it is less readable and slower than f-strings.

Example: Basic format() Usage

Output:

The ticket price is 75 rupees
Try it Yourself

Example: Formatting with Decimals

Output:

Temperature: 36.7°C
Try it Yourself

Formatting Multiple Values

Example: Multiple Placeholders

Output:

Order: 4 items | ID: 210 | Cost: 349.50
Try it Yourself

Using Index Numbers

Index numbers ensure values appear in the correct position.

Example: Indexed Placeholders

Output:

Item Headphones costs 499.99 rupees
Try it Yourself

Example: Reusing the Same Value

Output:

Rahul is 21 years old. Next year, Rahul will be 21.
Try it Yourself

Named Placeholders

Named placeholders improve readability when working with many values.

Example: Named Indexes

Output:

Car: Toyota, Model: Corolla
Try it Yourself

Our website uses cookies to enhance your experience. Learn More
Accept !