Python Strings – Part 2

A digital landscape illustration shows a beige fabric banner with the words “Python Strings” written across it. The material is ripped down the centre, with large visible stitches crudely sewing the two halves back together, symbolising fixing or escaping characters in strings. The background is simple and muted, drawing attention to the torn fabric.

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:

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:

Try this

  1. Print "It's my friend's birthday!" using single quotes and escapes.
  2. Print two lines of text with \n.
  3. 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 '''...''').

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:

It gives you all characters from start up to, but not including, stop.

Example:

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:

Try this

  1. Make fruit = "strawberry".
  2. Print the first 5 letters.
  3. Print the last 3 letters.
  4. 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.

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 of word.

Summary

  • Use escape characters like \', \", and \n to 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.

Main Topic

Python Fundamentals

A colourful cartoon-style landscape illustration shows a person sitting at a computer terminal, looking overwhelmed by the amount of information on the screen. Papers and symbols float around them, representing different programming concepts. The word “Fundamentals” is clearly written above the scene, highlighting the focus on beginner Python basics.

Beginner’s guide to Python fundamentals: strings, numbers, booleans, syntax, and text manipulation explained simply and clearly.

Other Tutorials in this Topic

A playful cartoon-style landscape illustration shows a large elephant on the left and a small cat on the right. Between them, a bold greater than ( > ) symbol points toward the cat, representing a Python comparison operator in a fun way.

Python Operators

Beginner’s guide to Python operators: arithmetic, comparison, logical, assignment, membership, and identity explained with examples.

A colourful cartoon-style landscape illustration shows a smiling robot holding up two placards: one clearly labelled True and the other False. The background is bright and cheerful, symbolising the concept of Boolean values in Python programming.

Python Booleans

Learn Python Booleans: True or False values, comparisons, logic operators, and decision-making with simple examples.

ChatGPT said: A colourful cartoon-style landscape illustration shows two large labelled containers: one marked Integers filled with whole numbers like 7, 0, and -3, and another marked Floats filled with decimal numbers like 3.14, -0.5, and 2.0. A cheerful character is sorting the numbers into the correct containers, visually explaining the difference between integers and floating-point numbers in Python.

Python Numbers

Learn Python numbers: integers and floats, arithmetic operations, conversions, rounding, precision issues, and practical exercises for beginners…

A colourful cartoon-style landscape illustration shows the text “python ” written across the scene. Above the gap, a labelled box with the word “strings” is tilted, pouring its contents into the space, visually representing a variable being used to fill in a placeholder.

Python Strings – Part 4

Discover Python string formatting: use f-strings, .format(), and number formatting to neatly combine variables and text into…

A colourful cartoon-style landscape illustration shows the words “python strings” in lowercase being fed into a large whimsical machine. On the other side, the text emerges transformed into “PYTHON STRINGS” in uppercase letters, symbolising string methods like .upper(). The machine has gears and pipes, giving a fun and playful feel.

Python Strings – Part 3

Learn Python string methods: change case, remove spaces, replace text, split and join words, and check content…

A colourful cartoon-style landscape illustration shows festive bunting strung across the top, with triangular flags spelling out “Python Strings” in bright, playful letters. The background is light and cheerful, giving a fun and welcoming atmosphere.

Python Strings – Part 1

Learn Python strings: text in quotes, printing, joining with +, counting length, and accessing characters by position…

A colourful cartoon-style illustration shows a confused student with orange hair shrugging in front of a computer screen. The screen displays a Python function with a missing indentation error:

Python Syntax

Learn Python syntax basics with clear examples and beginner-friendly practice exercises.

A cartoon-style illustration shows a person’s hands typing on a laptop running Visual Studio Code in dark mode. The screen displays a Python file with the code print("Hello, world!"), alongside a large, colourful Python logo. The background is bright blue, creating a vibrant and engaging learning atmosphere.

Start Coding Python with VS Code

Beginner’s guide to installing Python, setting up VS Code, and getting ready for coding.

Python Variables

Python Variables

Learn Python variables with simple explanations, real-world analogies, practice tasks to build confidence using data in programs.