Skip to content
CodeItRaw
Strings

Lesson 1/3

A text (str) is much like a list: its characters are in order, are read by index, can be sliced, walked with for and measured with len. Everything you learnt for lists holds here: indices start at 0, text[-1] is the last character, text[1:4] leaves out its end.

word = "Kayak"
word[0] # "K"
word[::-1] # "kayaK"
word.lower() # "kayak"
word[1:3] # "ay"
[::-1] reverses the text (the third number is the step, and -1 walks backwards). lower() makes it lowercase. Both give a new text; word stays as it was.

You can check whether a text reads the same backwards (a palindrome) in two ways: build the reverse and compare (text == text[::-1], short, and it makes a copy), or walk in from both ends comparing characters (no copy, and it stops at the first difference).

Tasks

Tasks open in order. Solve them all and the next lesson opens.

This lesson's tasks open when the lessons before it are finished. You can read the explanation now.

  1. 01

    Text Slices

    Code reading · Predict the Output

  2. 02

    Text Palindrome

    Function

  3. 03

    Is It a Palindrome?

    Code reading · Break the Code

If you would rather not wait for the order, every problem is open without locks: Problem list