Working with Words


Last time you printed words and stored them in variables. Today you learn to do things to words. In Python, a piece of text is called a string, and strings come with a bunch of handy tricks.

Gluing words together

The + sign joins two strings into one. This is called concatenation (fancy word, simple idea — stick them together):

 

Notice the " " in the middle — that’s a string with just a space, so the name doesn’t come out as AdaLovelace. You have to add spaces yourself.

Try it 🎯

  1. Put your own first and last name in.
  2. Build the name backwards: last + ", " + first (like a class list).

Measuring a word

len(word) tells you how many characters are in a string:

 

Try it 🎯

Ask the user for a word with input, then tell them how long it is.

LOUD and quiet

Strings have built-in methods — little commands you attach with a dot:

  • .upper() makes it ALL CAPS.
  • .lower() makes it all lowercase.
 

A .method() doesn’t change the original string — it hands back a new one. That’s why we print the result.

Repeating a string

Multiplying a string by a number repeats it. Great for drawing lines of text or being dramatic:

 

Try it 🎯

Make a “fence” by printing "|--" repeated 10 times.

Putting it together: a shout machine

Ask for a word and shout it back, with excitement:

 

Predict it 🔮

What does this print? Think about what + does with two strings. (Hint: it does not do math here.)

 

(It prints 34, not 7! When both sides are strings, + glues them. Those are the characters 3 and 4, not the numbers. Doing actual math is next lesson.)

Fix the bug 🐞

This is supposed to print Hello, friend but the words are jammed together. Add the missing space:

 

(Glue a space in: greeting + " " + who — or use a comma in print, which adds the space for you.)

Your mission 🚀

Write a “name styler”: ask the user for their name, then print it three ways — all caps, all lowercase, and repeated three times:

 

What you learned today

  • A string is text; + glues strings together (remember the spaces!).
  • len(word) counts characters.
  • .upper() and .lower() change the case (and hand back a new string).
  • "abc" * 3 repeats a string.

Next time: numbers and math — and the important difference between the text "3" and the number 3. 🔢

Comments