Legends ofPythos
Claim your name
Working with Data

Strings, properly

Lesson 11 of 14

Watch the lesson1:17 · with Torsten
You know how to print strings and slice them with [start:end]. Today we unlock the full toolkit: methods that let you split text apart, join it back together, search inside it, clean up extra spaces, and even reverse it. These are the moves a real Python developer uses every day.

Splitting and joining

csv = "apple,banana,cherry"
parts = csv.split(",")
print(parts)

back_together = ".".join(["2024", "12", "5"])
split() -> list, join() -> string. They are opposites.
split(sep) cuts a string at every sep and returns a list. If you omit the argument, it splits on any whitespace. .join(iterable) does the reverse: it glues strings together with this string as the separator.

Searching inside a string

word = "pythonic"

idx = word.find("thon")
is_prefix = word.startswith("pyt")
is_suffix = word.endswith("nic")
.find(), .startswith(), and .endswith()
find(sub) returns the index of sub or -1 if it is not there. .startswith(prefix) and .endswith(suffix) return a plain bool. They are cleaner than slicing when you only care about one end.

Slicing with step, reversing, and stripping

s = "abcdefgh"
every_other = s[::2]
reversed_s  = s[::-1]
[start:end:step] — the third slot lets you skip characters.
s[start:end:step] walks through s, taking one character every step. The special slice [::-1] is the idiomatic way to reverse a string. .strip() removes whitespace (and newlines) from both ends; use .lstrip() or .rstrip() for just one side.

Replacing and checking content

msg = "hello world"
cleaned = msg.replace("world", "python")

"12345".isdigit()
"abcde".isalpha()
.replace() is non-destructive; .isdigit()/isalpha() return bools.

Your turn

0 of 3 solved

Exercise 1

+30 XP
Write a function clean_and_split(text) that takes a string, strips leading/trailing whitespace, lowercases it (use .lower()), replaces every comma with a space using .replace(), and then splits on spaces. Return the resulting list.
def clean_and_split(text):
    pass

Run your code to check it against the tests.

Exercise 2

+30 XP
Write a function is_palindrome(s) that returns True if the string (after stripping whitespace and lowercasing) reads the same forwards as backwards. Use slicing with [::-1] to reverse it.
def is_palindrome(s):
    pass

Run your code to check it against the tests.

Exercise 3

+30 XP
Write a function parse_ids(raw) that takes a string of comma-separated values, strips each item, keeps only items where .isdigit() is True, and returns them as a list of integers. Ignore any non-digit entries.
def parse_ids(raw):
    pass

Run your code to check it against the tests.