Python F-Strings
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

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

Welcome to Python programming

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

Your final score is 88

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

Average rating: 7.46

Example: Format a Value Directly

Success rate: 92.3%

Performing Operations in F-Strings

Python expressions can be evaluated directly inside placeholders.

Example: Arithmetic Operation

Total cost: 1800 rupees

Example: Calculation Using Variables

Payable amount: 450.0

Conditional Logic Inside F-Strings

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

Example: Conditional Output

Result: Pass

Calling Functions in F-Strings

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

Example: Built-in Function

I live in Chennai

Example: User-Defined Function

Distance: 6.21 miles

Advanced Formatting Options

Example: Thousand Separator

Population: 14,500,000

Example: Percentage Format

Completed: 88%

Common Formatting Types

Type Description
< Left align
> Right align
^ Center align
+ Show sign
, Thousand separator
_ Underscore separator
b Binary
d Decimal
f Fixed-point
e Scientific
o Octal
x / X Hexadecimal
% 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

The ticket price is 75 rupees

Example: Formatting with Decimals

Temperature: 36.7°C

Formatting Multiple Values

Example: Multiple Placeholders

Order: 4 items | ID: 210 | Cost: 349.50

Using Index Numbers

Index numbers ensure values appear in the correct position.

Example: Indexed Placeholders

Item Headphones costs 499.99 rupees

Example: Reusing the Same Value

Rahul is 21 years old. Next year, Rahul will be 21.

Named Placeholders

Named placeholders improve readability when working with many values.

Example: Named Indexes

Car: Toyota, Model: Corolla

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