---
title: "Explain the Python String Formatting"  
description: "Explain the Python String Formatting"  
author: "Anubhav Sharma"  
published: 2025-10-15  
updated: 2025-10-21  
canonical: https://www.mindstick.com/forum/161959/explain-the-python-string-formatting  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 3 minutes  

---

# Explain the Python String Formatting

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) Formatting**

## Replies

### Reply by ICSM Computer

> Python **string formatting** allows you to **insert variables or expressions** into strings dynamically — instead of concatenating manually.

There are **four main ways** to format strings in Python, each evolving with the language.

## 1. Old Style Formatting (`%` Operator)

This is similar to C’s `printf` formatting.

```python
name = "Anna"
age = 25
print("My name is %s and I am %d years old." % (name, age))
```

## Output:

```plaintext
My name is Anna and I am 25 years old.
```

### Common format specifiers:

| Specifier | Meaning | Example Output |
| --- | --- | --- |
| `%s` | String | `'Anna'` |
| `%d` | Integer | `25` |
| `%f` | Floating-point | `25.000000` |
| `%.2f` | 2 decimal places | `25.00` |

## 2. `str.format()` Method (Introduced in Python 2.6+)

This method uses `{}` placeholders inside strings.

```python
name = "Anna"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
```

## Output:

```plaintext
My name is Anna and I am 25 years old.
```

You can also use **indexes** or **named arguments**:

```python
print("My name is {0} and I am {1} years old. {0} loves Python.".format(name, age))
print("My name is {name} and I am {age} years old.".format(name="Anna", age=25))
```

## Formatting numbers:

```python
pi = 3.1415926
print("Pi rounded to 2 decimals: {:.2f}".format(pi))
```

## Output:

```plaintext
Pi rounded to 2 decimals: 3.14
```

## 3. Formatted String Literals (f-Strings) — Python 3.6+

This is the **modern and most readable way**.

You prefix the string with `f` or `F`, and expressions inside `{}` are evaluated directly.

```python
name = "Anna"
age = 25
print(f"My name is {name} and I am {age} years old.")
```

## Output:

```plaintext
My name is Anna and I am 25 years old.
```

You can also use **expressions** inside `{}`:

```python
print(f"Next year, I’ll be {age + 1}.")
```

## Number formatting:

```python
pi = 3.1415926
print(f"Pi rounded to 3 decimals: {pi:.3f}")
```

## Output:

```plaintext
Pi rounded to 3 decimals: 3.142
```

## Alignment and width:

```python
name = "Anna"
print(f"|{name:<10}|")  # Left align
print(f"|{name:^10}|")  # Center align
print(f"|{name:>10}|")  # Right align
```

## Output:

```plaintext
|Anna      |
|   Anna   |
|      Anna|
```

## 4. Template Strings (from `string` module)

Useful when you want **safe substitutions** (e.g., user input).

```python
from string import Template

t = Template("Hello, $name! You have $$${amount}.")
msg = t.substitute(name="Anna", amount=100)
print(msg)
```

## Output:

```plaintext
Hello, Anna! You have $100.
```

If you use `.safe_substitute()` instead, it **won’t raise an error** if a placeholder is missing.

## Comparison Summary

| Method | Example | Best For |
| --- | --- | --- |
| `%` | `"Hello %s" % name` | Legacy code |
| `.format()` | `"Hello {}".format(name)` | Python 2/3 compatibility |
| `f""` | `f"Hello {name}"` | Modern, fast, readable |
| `Template` | `Template("Hello $name")` | Safe user input formatting |

## Example Summary

```python
name = "Anna"
age = 25
pi = 3.14159

# 1. Old Style
print("Name: %s, Age: %d" % (name, age))

# 2. str.format()
print("Name: {}, Age: {}".format(name, age))

# 3. f-String
print(f"Name: {name}, Age: {age}, Pi: {pi:.2f}")

# 4. Template
from string import Template
t = Template("Name: $name, Age: $age")
print(t.substitute(name=name, age=age))
```


---

Original Source: https://www.mindstick.com/forum/161959/explain-the-python-string-formatting

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
