Python Learning Series · @onlysamarrr · Day 7 / 30

Day 7: f-strings and Slicing

Day 7 teaches f-strings and slicing. An f-string puts variables inside text with {}, and slicing [start:end:step] takes part of a string. Indexing starts at 0.

Phase: Beginner · Reels: 31/450 – 35/450

Quick Answer

Day 7 teaches f-strings and slicing. An f-string puts variables inside text with {}, and slicing [start:end:step] takes part of a string. Indexing starts at 0.

What You Will Learn

Code

name = "Arhan"
score = 92.456
s = "python"
print(f"{name}: {score:.1f}%")
print(f"{name=}")
print(s[0], s[-1])
print(s[0:3])
print(s[::2])
print(s[::-1])

Output

Arhan: 92.5%
name='Arhan'
p n
pyt
pto
nohtyp

Code Explained, Line by Line

  1. f"{name}: {score:.1f}%" puts variables inside text; :.1f means one decimal place.
  2. f"{name=}" shows both the variable name and its value, which is handy for debugging.
  3. s[0] is the first letter and s[-1] is the last letter, because indexing starts at 0.
  4. s[0:3] takes letters at positions 0, 1 and 2. The end position (3) is not included.
  5. s[::2] takes every second letter, and s[::-1] reverses the whole string.
Common Mistake: The first index is 0, not 1. In s = "python", s[1] is 'y', not 'p'.

Real-Life Use

Formatting messages, prices and reports, and reversing or slicing text, all use f-strings and slicing.

Practice Task: Reverse any string with s[::-1] and explain why a step of -1 walks backwards.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. In s = "python", what is s[1]?

Q2. What does "python"[::-1] give?

Q3. What does f"{2 + 3}" produce?

Today's 5 Reels (from the 90-Day Plan Excel)

31/450Conceptf-string se formatting
32/450Live demoSlicing [start:end:step]
33/450Common mistakeIndex 0 se start hota
34/450QuizQuiz: string reverse karo
35/450Interview / real lifeInterview: s[::-1] kaise kaam karta
← Day 6Day 8 →