TalentJR

🐍 Python for kids · Class 5 · Lesson 5 of 18 · Free

String Operations & Formatting

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Join and repeat strings, and count letters with len()
  • Pick out letters and pieces with indexing and slicing
  • Build neat sentences with f-strings

Your name is made of letters, one after another, like beads on a string. That is exactly why Python calls text a string. Today you will join strings, cut them, change them and fill them into neat sentences.

How do I join and repeat strings in Python?

The + sign joins two strings into one. This is called concatenation.

+ does not add a space for you. If you want a space, join one yourself: " ".

The * sign repeats a string. "-" * 20 makes a line of twenty dashes, great for borders.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶first = "Virat"
2last = "Kohli"
3full = first + " " + last
4print(full)
5print("=" * 20)
6print("Go " * 3 + "India!")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Virat Kohli
====================
Go Go Go India!

How do I find the length of a string?

len() counts how many characters a string has. A character is any single letter, digit, space or sign.

Spaces count too! "New Delhi" has 8 letters but len() gives 9.

len() is useful for checks, like whether a password is long enough.

🐍 Python — you can edit this code
Output
9
6
0

💡 "" is an empty string. It has no characters, so its length is 0.

What do upper(), lower(), strip() and replace() do?

Strings have built-in helpers called methods. You write a dot after the string, then the method name and brackets.

upper() makes every letter capital and lower() makes them small. strip() removes extra spaces from both ends.

replace() swaps one piece of text for another. These methods make a new string; the old one stays the same.

🐍 Python — you can edit this code
Output
  CHENNAI EXPRESS  
  chennai express  
Chennai Express
  Chennai Mail  

💡 strip() is very handy with input(). Users often type an extra space by mistake.

How does indexing pick one letter?

Each character in a string has a position number called an index. Python starts counting from 0, not 1.

Write the string name, then the index in square brackets. word[0] is the first letter.

Negative numbers count from the end. word[-1] is always the last letter.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶word = "PYTHON"
2print(word[0])
3print(word[1])
4print(word[5])
5print(word[-1])
6print(word[-2])
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
P
Y
N
N
O

💡 In "PYTHON": P is 0, Y is 1, T is 2, H is 3, O is 4, N is 5.

How does slicing cut out part of a string?

A slice takes a piece of a string. You write two numbers: word[start:stop].

The slice starts at start but stops just before stop. So word[0:3] gives positions 0, 1 and 2.

Leave out the start to begin at the beginning. Leave out the stop to go to the end.

🐍 Python — you can edit this code
Output
SUN
FLOWER
SUN
FLOWER
NFLO

💡 A neat trick: word[:3] + word[3:] always gives the whole word back.

What are f-strings in Python?

Joining many pieces with + gets messy. An f-string is a cleaner way to build a sentence.

Put the letter f just before the opening quote. Then write any variable inside curly brackets { }.

Python fills in each value. You can even put a small sum inside the brackets.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶name = "Meera"
2maths = 88
3science = 92
4print(f"{name} scored {maths} in Maths.")
5print(f"Total marks: {maths + science}")
6print(f"Name in capitals: {name.upper()}")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Meera scored 88 in Maths.
Total marks: 180
Name in capitals: MEERA

💡 f-strings can mix text and numbers safely. With +, you can only join strings to strings.

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

print("ab" * 3)

Question 2. What does this print?

word = "CRICKET"
print(word[1:4])

Question 3. What does this print?

s = "  Hi  "
print(len(s.strip()))

🧩 Your challenge

Make a name badge. Ask the user for their full name. Remove extra spaces, then print a border, the name in capitals, its first letter, and how many characters it has. Use f-strings.

Expected output (with the inputs given):
Your full name:   Priya Sharma  
************************
NAME: PRIYA SHARMA
First letter: P
Characters: 12
************************
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

age = 10
print("Age: " + age)

TypeError: can only concatenate str (not "int") to str

✅ Right

age = 10
print(f"Age: {age}")

+ can only join a string to a string. An f-string can mix text and numbers.

❌ Wrong

city = "Delhi"
print(city[5])

IndexError: string index out of range

✅ Right

city = "Delhi"
print(city[4])

"Delhi" has 5 letters at positions 0 to 4. There is no position 5.

❌ Wrong

name = "Arjun"
print("Hello {name}")

No error – but it prints Hello {name} instead of Hello Arjun.

✅ Right

name = "Arjun"
print(f"Hello {name}")

Without the f before the quote, curly brackets are just ordinary text.

📌 Remember

  • + joins strings and * repeats them.
  • len() counts characters, including spaces.
  • upper(), lower(), strip() and replace() give a changed copy of a string.
  • Indexes start at 0; word[-1] is the last character.
  • word[start:stop] slices a piece and stops just before stop.
  • f-strings fill values into text: f"Hi {name}".

Finished the quiz and the challenge?

Questions students ask

Why does Python start counting from 0?

The index tells how many steps you move from the start. The first letter is zero steps away, so its index is 0.

What is an f-string in Python?

An f-string is a string with f before the quote, like f"Hi {name}". Python replaces each { } part with the value of what is inside.

Does upper() change the original string?

No. Strings cannot be changed in Python. upper() makes a new string. To keep it, store it: name = name.upper().

What is the difference between indexing and slicing?

Indexing like word[2] gives one character. Slicing like word[2:5] gives a piece of the string, from position 2 up to 4.