🐍 Lesson 3: Working with Text

Strings — joining, slicing, and transforming words

← Back to Python Basics

🔗 Joining strings together

You can glue strings together with the + sign. This is called concatenation.

first = "Sky" last = "Walker" full = first + last print(full) # SkyWalker

Need a space? Add one inside quotes:

full = first + " " + last print(full) # Sky Walker

✨ f-strings — the easy way to build messages

An f-string lets you drop variables right inside text. Put an f before the quotes, and wrap variable names in { }.

name = "Maya" age = 11 print(f"Hi {name}, you are {age} years old!") # Hi Maya, you are 11 years old!

📌 Why f-strings rule

No more juggling + signs and quotes. f-strings are the modern, clean way to combine text and variables. You'll use them constantly!

🔢 Strings have a length

Count the characters (including spaces) with len():

word = "python" print(len(word)) # 6

📍 Picking characters (indexing)

Each character has a position number called an index. Python starts counting at 0!

word = "PYTHON" # 012345 print(word[0]) # P print(word[1]) # Y print(word[-1]) # N (negative counts from the end)

✂️ Slicing — grabbing a piece

Take a chunk with [start:end]. The start is included, the end is not.

word = "PYTHON" print(word[0:3]) # PYT print(word[2:]) # THON (to the end) print(word[:4]) # PYTH (from the start)

🛠️ Handy string methods

Strings come with built-in superpowers you call with a dot:

shout = "hello" print(shout.upper()) # HELLO print("LOUD".lower()) # loud print(" hi ".strip()) # hi (removes outside spaces) print("cat".replace("c", "b")) # bat print("a,b,c".split(",")) # ['a', 'b', 'c']

✅ What you learned

  • Join strings with +, or better — use f-strings.
  • len() counts characters.
  • Indexing starts at 0; [-1] is the last character.
  • Slice with [start:end].
  • Methods like .upper(), .lower(), .replace() transform text.