Skip to content

Commit 5f82f6f

Browse files
Merge pull request #28 from harshitnagar22/main
docs: add f-string section to what-is-a-variable concept (#14)
2 parents 653dae8 + a2e37f6 commit 5f82f6f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

concepts/what-is-a-variable.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ age = 30
2020
is_student = True
2121
```
2222

23+
## Embedding variables in strings (f-strings)
24+
25+
You can put variables directly inside a string by placing an `f` before the opening quote, then wrapping any variable name in curly braces `{}` where you want its value to appear. This is the most common and readable way to format strings in modern Python.
26+
27+
```python
28+
name = "Alice"
29+
age = 30
30+
greeting = f"Hello, {name}! You are {age} years old."
31+
print(greeting) # Output: Hello, Alice! You are 30 years old.
32+
```
33+
34+
The `f` tells Python: *"treat anything in curly braces as a variable, not plain text."* Without the `f`, `{name}` is just printed as the literal characters `{name}`.
35+
36+
```python
37+
print(f"Hello, {name}") # Output: Hello, Alice ✅
38+
print("Hello, {name}") # Output: Hello, {name} ❌ (missing f)
39+
```
40+
2341
## Visualize It
2442

2543
See how Python stores variables in memory step by step:

0 commit comments

Comments
 (0)