PyPyPath

Module 4: Strings in Python

String Methods

Strings come with built-in methods — handy tools for cleaning and changing text. You will use these a lot when preparing data.

About 12 minutes

Case and whitespace

Example code
name = "  python  "
print(name.upper())      # "  PYTHON  "
print(name.lower())      # "  python  "
print(name.strip())      # "python" (trims spaces)

split and join

Example code
csv_line = "apple,banana,mango"
fruits = csv_line.split(",")
print(fruits)

joined = "-".join(fruits)
print(joined)

find and replace

Example code
text = "I love Python"
print(text.replace("love", "enjoy"))
print("Python" in text)  # True
  • len(s) — length of string
  • s.startswith("Hi") — check beginning
  • s.endswith(".csv") — check file names
Practice

Clean messy = ' HELLO world ': strip, lower, then replace 'world' with 'Python'.

Key takeaways

  • Methods use dot: text.upper(), text.split(',').
  • strip() removes extra spaces — common in real data.
  • split/join help work with comma-separated values.

Quick check: String Methods

Question 1 of 2

" hi ".strip() returns?