String Formatting

Python · Strings

Theory

An f-string is a string literal with an f prefix in which anything inside curly braces is evaluated as Python code and inserted as text. f'{name} earns {salary:,}' becomes 'Asha earns 65,000'. The expression can be a variable, arithmetic or even a method call like {name.upper()}, and it is evaluated at the moment the line runs, so the string always reflects current values.

After the expression you can add a colon and a format specifier to control how the value looks: :, adds thousands separators, :.2f rounds to 2 decimal places, :.1% shows a fraction as a percentage, and :>4 right-aligns in a field 4 characters wide. {salary=} prints both the expression and its value, which is handy when debugging. To show a literal brace, double it: {{ or }}. Older styles, .format() and % formatting, still work and appear in older code.

Why we use it

Reports, logs and messages mix fixed text with changing values. f-strings make that readable and handle number formatting (commas, decimals, percentages) without manual str() conversions or concatenation.

When to use it

Use f-strings for nearly all formatting in Python 3 code. Use str.format() when the template is stored separately from the values (for example in a config file). Never build SQL queries with f-strings from user input; use parameters.

How it works

  1. Python meets f'{name} earns {salary:,}'
  2. Split the literal into fixed text and {...} fields
  3. Evaluate each field's expression: name -> 'Asha', salary -> 65000
  4. Does the field have a :format spec? Yes: Format the value with it, e.g. 65,000. No: Convert the value with str().
  5. Join all the pieces into one new string
  6. Output: Asha earns 65,000

Syntax

f'{expression}'

Example

name = 'Asha'
salary = 65000
print(f'{name} earns {salary:,}')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic