Python String Formatting

Python · Tooling & Errors

Theory

String formatting builds text from values. The modern way is the f-string: put f before the quotes and write any expression inside braces, f'{name} earns {salary}'. Python evaluates each expression and inserts the result. After a colon you can add a format specifier that controls how the value looks.

Useful specifiers: :, adds thousands separators (65000 -> 65,000), :.2f rounds to two decimals (3.14159 -> 3.14), :.1% shows a fraction as a percentage (0.256 -> 25.6%), :>8 right-aligns in 8 characters and :03d pads an integer with zeros (7 -> 007). Older styles still appear in existing code: str.format() ('{} earns {}'.format(name, salary)) and % formatting ('%s earns %d' % (name, salary)).

Why we use it

Reports, logs and messages need numbers shown cleanly - currency with separators, rounded ratios, aligned columns. f-strings do this inline and readably without manual str() conversion and 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, and never build SQL queries with f-strings - use parameterised queries.

How it works

  1. Write f'{name} earns {salary:,}'
  2. Find each {...} placeholder
  3. Evaluate the expression inside the braces
  4. Is there a format spec after ':'? Yes: Apply it, e.g. add commas. No: Use the default str() form.
  5. Final string: 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