In Part 1, you learnt how to create strings, print them, join them together, count their length, and pick out characters by position. Now let’s explore some new tricks that will make your strings even more powerful.
Escaping characters
Sometimes you want to include quotes or special characters inside your string. But if you write them directly, Python gets confused.
Example:
print("It's sunny today") # ✅ Works fine with double quotes
print('It\'s sunny today') # ✅ Using an escape character
The backslash \ tells Python: “Treat the next character as text, not as code.”
Some common escape codes:
\'→ single quote\"→ double quote\\→ backslash\n→ new line (start a new line of text)\t→ tab (a few spaces)
Example:
print("He said \"Hello!\"")
print("First line\nSecond line")
print("A\tB\tC")
Try this
- Print
"It's my friend's birthday!"using single quotes and escapes. - Print two lines of text with
\n. - Print a table with three words spaced out using
\t.
Multi-line strings
If you want to write a block of text across several lines, use triple quotes ("""...""" or '''...''').
poem = """Roses are red,
Violets are blue,
Python is fun,
And so are you!"""
print(poem)
This saves you from having to type \n for each new line.
Try this
Write a short two-line story inside triple quotes and print it.
Slicing strings (substrings)
You already know how to grab a single character using its position, like word[0].
But you can also grab a part of the string (called a substring) using a slice.
The format is:
string[start:stop]
It gives you all characters from start up to, but not including, stop.
Example:
word = "fantastic"
print(word[0:3]) # 'fan' (from index 0 up to 3)
print(word[3:7]) # 'tast'
If you leave out the start or stop, Python assumes the beginning or end:
print(word[:4]) # 'fant' (start to index 4)
print(word[4:]) # 'astic' (from index 4 to the end)
You can even use negative indexes to count backwards:
print(word[-3:]) # 'tic' (last 3 letters)
Try this
- Make
fruit = "strawberry". - Print the first 5 letters.
- Print the last 3 letters.
- Print everything from the 3rd letter to the end.
Strings are sequences (like lists)
Strings are very similar to lists of characters. You can loop through them or slice them just like you would with a list.
But remember: strings are immutable — you can’t change individual characters.
word = "cat"
# word[0] = "b" # ❌ Error
word = "b" + word[1:] # ✅ Make a new string
print(word) # 'bat'
Try this
Take word = "dog".
- Try changing the first letter directly (
word[0] = "l"). What happens? - Now create a new word
"log"by combining"l"with the rest ofword.
Summary
- Use escape characters like
\',\", and\nto include quotes and special formatting. - Use triple quotes for multi-line strings.
- Use slicing (
word[start:stop]) to grab parts of a string. - Strings are immutable, so you can’t change characters directly — but you can make new strings from old ones.
✅ With these new tools, you can handle strings with quotes, new lines, long text, and even cut them up into pieces.










