Learning Goals
3 minBy the end of this lesson you can:
- Pick between single, double, and triple quotes for a string.
- Join strings with
+and repeat them with*. - Measure any string's length with
len().
Warm-Up
5 minLast lesson we used + and * on numbers. Today we use the same symbols on words.
Quick predictions — what does Python show for these?
print("ha" + "ha") print("ha" * 5)
Write your guesses on paper before scrolling on. The same symbols can do something a little different when they meet a string.
In Python, the meaning of + depends on what is on either side of it. Numbers add. Strings join.
New Concept · Three Superpowers for Strings
12 minPart 1 · Three quote styles
Strings can be wrapped in three different ways:
- Single quotes
'…' - Double quotes
"…" - Triple quotes
"""…"""for multi-line text
Single and double do exactly the same thing. Pick one style and stick with it inside a file.
Use the other style when your string already contains that quote character:
print("She said 'Selamat datang!' with a smile.") print('Her name is "Aisyah".')
Or escape the quote with a backslash, the trick we met in PY-L1-02:
print("She said \"Selamat datang!\" with a smile.")
Part 2 · Joining strings with +
Two strings can be joined with +. The result is one new string:
greeting = "Hello, " + "Aisyah" + "!" print(greeting)
Joining strings is called concatenation.
Both sides of + must be strings. Mixing types fails:
age = 12 print("I am " + age)
To fix it, convert the number with str() first:
print("I am " + str(age))
Part 3 · Repeating strings with *
Multiplying a string by an integer repeats it that many times:
print("ha" * 5) print("-" * 30) print(">_<" * 3)
Tidy way to make dividers, rows of stars, ASCII art, and more.
Part 4 · Measuring with len()
The len() function returns the number of characters in a string. The answer is an int.
print(len("Penang")) print(len("Hello, Aisyah!")) print(len(""))
It counts every character — letters, spaces, digits, and symbols all count as one.
Spaces matter. "hi " has length 3, not 2. The trailing space is a real character.
Worked Example · A Self-Sizing Banner
12 minWe will build a banner that resizes itself to match the greeting inside it. Save this as banner.py.
Code
# banner.py — a banner that resizes itself name = "Aisyah" # 1. Build the greeting by joining strings greeting = "Hello, " + name + "!" # 2. Find how long it is greeting_length = len(greeting) # 3. Build a divider line that matches exactly divider = "=" * greeting_length # 4. Print the banner print(divider) print(greeting) print(divider) print() print("Your greeting is", greeting_length, "characters long.")
Run it. You should see:
Output
============== Hello, Aisyah! ============== Your greeting is 14 characters long.
Now change name = "Aisyah" to name = "Wei Jie" and re-run. The divider auto-resizes — that's the magic of measuring before drawing.
Mixing strings and numbers — meet str()
name = "Aiman" age = 13 # str(age) turns the number 13 into the string "13" message = "Hi! I'm " + name + " and I'm " + str(age) + " years old." print(message) print("Length:", len(message))
Output
Hi! I'm Aiman and I'm 13 years old. Length: 35
You can also do print("I'm", name, "and I'm", age) with commas — Python adds the spaces. Use + when you want to store the joined message in a variable.
Try It Yourself
13 minType each task into your editor. Run it. Fix any typos before moving on.
Print a row of 20 stars, your name on its own line, and another row of 20 stars. Use "*" * 20.
Hint
Three print() lines: one for the divider, one for your name, one for the divider again.
Make a variable word = "Penang". Then print three lines:
- The word's length using
len(). - The word repeated 3 times in a row, using
*. - A friendly sentence built with
+, like"My favourite city is " + word + "!".
Hint
Three separate print() calls. The first uses len(), the second uses *, the third uses +.
Create three variables for the names of three places you know — for example a school, a market, and a mosque or temple. Print the length of each. Then use + to add the three lengths together and print the total.
Hint
total = len(school) + len(market) + len(temple). The result is an int because len() always returns an int.
Mini-Challenge · School Banner
8 minBuild a tidy banner for a school using everything we learned today.
Your file must include:
- Three variables:
school_name,motto,year_established(an integer). - A
dividerbuilt with"*" * len(motto)so it always fits the motto. - An
establishedmessage built with+andstr(), like"Established in " + str(year_established). - A printed banner with: divider, school name, motto, established line, divider.
Stretch goal. Add an address variable using a triple-quoted multi-line string. Print it, then print its length — see what happens to \n when len() counts.
Show one possible solution
# school_banner.py — a tidy banner for a school school_name = "SMK Bukit Aman" motto = "Berkhidmat dengan Setia" year_established = 1965 # A divider that fits the motto exactly divider = "*" * len(motto) # An "established in YYYY" line, built by joining strings established = "Established in " + str(year_established) # Print the banner print(divider) print(school_name) print(motto) print(established) print(divider) # Stretch — multi-line address, and what len() makes of it address = """No. 12, Jalan Mawar, 50450 Kuala Lumpur""" print() print("Address:") print(address) print("Address length:", len(address))
Recap
3 minStrings are no longer just labels for print(). We can join them, repeat them, and measure them — and that's enough to build self-sizing banners and reports.
Vocabulary Card
- concatenation
- Joining strings with
+. Both sides must be strings. - str()
- Converts a value (like a number) into its string form.
- string repetition
"ab" * 3gives"ababab"— a string times an int.- len()
- Returns the number of characters in a string, as an int.
Homework
4 minCreate a new file called intro_banner.py that introduces you with a perfectly-fitted divider.
Rules:
- Variables for
full_name,age, andhometown. - Build a
messageusing+andstr()— for example"Hi, I am " + full_name + ", " + str(age) + ", from " + hometown + ".". - Use
len(message)to find its length. - Use
"-" * lengthto build a divider that matches the message exactly. - Print: divider, message, divider, then a final line stating the length.
Bring it to the next lesson — we'll see whose name has the longest banner!
Sample · intro_banner.py
# intro_banner.py — a personal banner with a perfectly-sized divider full_name = "Wei Jie" age = 12 hometown = "Ipoh" # Build the message by joining strings (str() turns the number into a string) message = "Hi, I am " + full_name + ", " + str(age) + ", from " + hometown + "." # Find its length, then build a divider that fits exactly length = len(message) divider = "-" * length # Print the banner print(divider) print(message) print(divider) print("That message is", length, "characters long.")
Yours can use any name, age, and hometown — Penang, Ipoh, KL, anywhere. As long as the divider's length matches len(message), you've nailed it.