Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.5 — Strings: The Basics
Python · Example 2 of 2

Strings never change in place

Methods return new strings. The last line fails on purpose.

A string (str) is a piece of text: a sequence of characters between quotes. You can measure it with len, pick characters by position, join and repeat strings, and test whether one is inside another. A string can never be changed in place.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Store your name in a variable, then print its length, its first letter and its last letter.

02

Print a line of 30 equals signs without typing them one by one.

03

Check whether the text "cat" appears in "concatenate", and print the answer.

Explain it without notes

01

What does it mean that a Python string is immutable, and how do you "modify" one?

02

How does string indexing work? What do s[0], s[-1] and len(s) give?

03

What are escape sequences and raw strings for?

04

What happens if you write "Age: " + 5, and how do you fix it?

Strings never change in place
Sign in to run this example in your browser.

Expected output

Python PYTHON
Jython Python
P 80 P
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    word[0] = "J"
    ~~~~^^^
TypeError: 'str' object does not support item assignment