> Source URL: /resources/f-strings.guide
# F-Strings

F-strings (formatted string literals) let you embed variables and expressions directly inside a string. Put an `f` before the opening quote and use `{}` to insert values.

## Example

```python
name = "Riley"
age = 17
print(f"Hi, I'm {name} and I'm {age} years old.")
# outputs: Hi, I'm Riley and I'm 17 years old.
```

## Why Use F-Strings?

Compare these three ways to build the same string:

```python
# String concatenation (clunky with numbers)
print("Score: " + str(score) + " points")

# Multiple print arguments (adds spaces)
print("Score:", score, "points")

# F-string (clean and readable)
print(f"Score: {score} points")
```

F-strings are usually the cleanest option.

## Embed Expressions

You can put any Python expression inside the curly braces:

```python
price = 19.99
print(f"Total with tax: {price * 1.08}")
# outputs: Total with tax: 21.5892

x = 5
print(f"{x} squared is {x ** 2}")
# outputs: 5 squared is 25
```

## String Methods in F-Strings

You can call string methods directly inside the braces:

```python
name = "alex"
print(f"Hello, {name.upper()}!")
# outputs: Hello, ALEX!

print(f"Hello, {name.title()}!")
# outputs: Hello, Alex!
```

## Text Alignment with `ljust()`, `rjust()`, `center()`

These methods pad a string to a specific width:

```python
label = "Name"
print(f"| {label.ljust(15)} |")   # left-align
# outputs: | Name            |

print(f"| {label.rjust(15)} |")   # right-align
# outputs: |            Name |

print(f"| {label.center(15)} |")  # center
# outputs: |      Name       |
```

**Common use case** — aligning columns:

```python
items = [("Apples", 3), ("Bananas", 12), ("Oranges", 7)]
for item, count in items:
    print(f"{item.ljust(10)} {count}")
```

**Output:**

```
Apples     3
Bananas    12
Oranges    7
```

## Combining with Other String Operations

F-strings work great with string multiplication for building patterns:

```python
width = 20
print(f"+{'-' * (width - 2)}+")
# outputs: +------------------+

rating = 4
stars = "★" * rating + "☆" * (5 - rating)
print(f"Rating: {stars}")
# outputs: Rating: ★★★★☆
```

## Common Mistakes

**Forgetting the `f` prefix**

```python
# Wrong - prints literal {name}
print("Hello, {name}!")

# Right
print(f"Hello, {name}!")
```

**Using quotes inside that match the outer quotes**

```python
# Wrong - syntax error
print(f"They said "hello"")

# Right - use different quotes
print(f'They said "hello"')
print(f"They said 'hello'")
```


---

## Backlinks

The following sources link to this document:

- [f-Strings Guide](/resources/resources.index.llm.md)
- [>ref: f-strings](/unit-1/projects/03-pet-py/pet-py.project.llm.md)
- [>ref: f-strings](/unit-1/projects/02-you-dot-py/you-dot-py.project.llm.md)
- [String Formatting Standards](/unit-2/projects/01-internal-review/01-internal-review.ticket.llm.md)
- [String Formatting Standards](/unit-2/projects/01-internal-review/oracle-handbook.guide.llm.md)
- [String Formatting Standards](/unit-2/projects/02-archive-audit/tickets/signals.ticket.llm.md)
- [String Formatting Standards](/unit-2/projects/02-archive-audit/tickets/simulations.ticket.llm.md)
- [String Formatting Standards](/unit-2/projects/02-archive-audit/oracle-handbook.guide.llm.md)
- [String Formatting Standards](/unit-2/projects/02-archive-audit/tickets/nodes.ticket.llm.md)
- [String Formatting Standards](/unit-2/projects/03-architects-index/tickets/signals.ticket.llm.md)
- [String Formatting Standards](/unit-2/projects/03-architects-index/oracle-handbook.guide.llm.md)
- [String Formatting Standards](/unit-2/projects/03-architects-index/tickets/nodes.ticket.llm.md)
- [String Formatting Standards](/unit-2/projects/03-architects-index/tickets/simulations.ticket.llm.md)
