L1·Foundations

✂️String Methods & Slicing

Indexing, slicing, split/join, strip, find and replace.

strslicesplitjoinstrip

§ 1Indexing

Strings are sequences. s[0] is the first character, s[-1] the last. Strings are immutable — you can read a character but never assign to s[0].

1s = "python"​
2print(s[0], s[-1]) # p n​

§ 2Slicing

s[start:stop:step] returns a new string. stop is excluded. Omitted parts default to the beginning, end and step 1. s[::-1] reverses.

1s = "python"​
2print(s[1:4]) # yth​
3print(s[:2]) # py​
4print(s[::-1]) # nohtyp​

§ 3Everyday methods

upper/lower/title change case. strip removes surrounding whitespace. split breaks into a list, join glues a list back together. replace swaps text, find returns an index or -1, startswith/endswith check prefixes.

1line = " a,b,c "​
2parts = line.strip().split(",")​
3print(parts) # ['a', 'b', 'c']​
4print("-".join(parts)) # a-b-c​

Worked example

1email = " Ada.Lovelace@Example.COM "​
2clean = email.strip().lower()​
3user, domain = clean.split("@")​
4print(user)​
5print(domain)​
6print(clean.endswith(".com"))​

Try it live · Python runs in your browser

Loading playground…
ChallengeCheck if a word is a palindrome, ignoring case and spaces.

Checkpoint quiz

Question 1 / 3
What is "hello"[1:3]?

Lesson test

Take an 8-question test on “String Methods & Slicing” — concepts, output prediction, bug spotting and interview questions.