HomeCourseModule 03 › Strings — text values and how to wrangle them

Strings — text values and how to wrangle them

Module 03 · Variables and Data Types9 min readBeginner

What you'll learn

  • Create strings with single, double, and triple quotes
  • Build strings with f-strings
  • Use the most common string methods

What's a string?

Text. Anything in quotes. Single or double — Python doesn't care, but pick one and be consistent.

name = "Alice"
greeting = 'Hello there'
long_text = """This is a multi-line string.
It can wrap across as many lines as you like."""

Joining strings together

first = "Alice"
last = "Smith"
full = first + " " + last     # "Alice Smith"

F-strings — the modern way

Put an f right before the opening quote, and you can drop variables straight inside {}:

name = "Alice"
sales = 1234.567
print(f"{name} sold ${sales:,.2f}")
# Alice sold $1,234.57

F-strings replaced the older "{} sold ${:.2f}".format(name, sales) style. Use f-strings everywhere.

String methods you'll use weekly

MethodWhat it doesExampleResult
.upper()UPPERCASE"hi".upper()"HI"
.lower()lowercase"Hi".lower()"hi"
.title()Title Case"alice smith".title()"Alice Smith"
.strip()Trim whitespace" abc ".strip()"abc"
.replace(a, b)Substitute"$1,200".replace(",", "")"$1200"
.split(",")Break into a list"a,b,c".split(",")["a","b","c"]
" ".join([...])Glue list back into a string" ".join(["a","b"])"a b"
.startswith("X")Boolean check"acme.csv".endswith(".csv")True

Slicing — pulling out a chunk

Like grabbing a substring with MID() or LEFT() in Excel.

name = "Anthropic"
name[0]      # 'A'        — first character
name[-1]     # 'c'        — last character
name[0:4]    # 'Anth'     — first four
name[4:]     # 'ropic'    — from index 4 to the end
name[::-1]   # 'cipohtnA' — reversed (a Python party trick)

Walkthrough: clean a messy column of names

Sample data

raw = "  alice SMITH  "

Clean step by step

step1 = raw.strip()           # "alice SMITH"
step2 = step1.lower()         # "alice smith"
step3 = step2.title()         # "Alice Smith"
print(step3)

Or chain it

cleaned = raw.strip().lower().title()
print(cleaned)   # Alice Smith

Chaining like this — calling one method on the result of another — is a Python superpower. You'll see it constantly in pandas.

Key takeaways

  • Strings live in quotes (single, double, or triple for multi-line).
  • F-strings are the modern way to mix variables into text: f"Hi {name}".
  • Methods like .upper(), .strip(), .replace() do most of the cleaning work.
  • Slicing — s[0:4] — pulls out substrings.

Clean a list of email addresses (on paper)

Given " Alice@Example.COM ", write the Python that turns it into a clean lowercase, trimmed email.

Answer
"  Alice@Example.COM ".strip().lower()
# 'alice@example.com'
📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.