Module 5: Dates, Strings & Logic30 min

String Methods

Progress Tracking

Log in to save this lesson and continue from where you left off.

Log in

Common String Methods

The .str accessor gives you access to every Python string method, but applied to an entire column at once. Here are the ones you’ll use most:

Python
worker["upper_name"] = worker["first_name"].str.upper()
worker["lower_name"] = worker["first_name"].str.lower()
worker["name_len"] = worker["first_name"].str.len()
worker[["first_name", "upper_name", "lower_name", "name_len"]]
  • .str.upper() / .str.lower() — case conversion
  • .str.strip() / .str.lstrip() / .str.rstrip() — remove whitespace
  • .str.len() — string length
  • .str.contains() — search for pattern
  • .str.startswith() / .str.endswith() — prefix/suffix check
  • .str.replace() — find and replace
  • .str.slice() / .str[:n] — extract substring by position
  • .str.split() — split by delimiter
  • .str.cat() — concatenate strings

String Concatenation

Python
# Simple concatenation with +
worker["full_name"] = (
    worker["first_name"] + " " + worker["last_name"]
)

# With formatting
worker["label"] = (
    "Employee: " + worker["first_name"]
    + " (" + worker["department"] + ")"
)
NaN Breaks Concatenation

If any column has NaN, the + operator returns NaN for that row. Use .fillna("") first, or use .str.cat(sep=" ", na_rep="") for NaN-safe concatenation.

Trimming Whitespace

Invisible trailing spaces often cause "why doesn’t my merge work?" bugs:

Python
# Clean before merging
df["name"] = df["name"].str.strip()

Combining Cleaned Text

1
Clean and Concatenate Names

Create a clean full name by stripping whitespace and uppercasing. The starter strips — add the concatenation.

Tables: worker

Substring Extraction

Python

# First 3 characters
worker["initials"] = worker["first_name"].str[:3]

# By position (0-indexed, unlike SQL)
worker["initials"] = worker["first_name"].str.slice(0, 3)
0-Indexed, Not 1

Python string positions are 0-indexed. .str.slice(0, 3) gets characters 0, 1, 2. SQL’s SUBSTRING(s, 1, 3) gets the same result but starts at 1.

Substring and Length

2
Extract Initials and Measure Length

For each employee, extract the first 3 characters of their first name and calculate name length. Sort by length descending.

Tables: worker

Case-Insensitive Filtering

Case inconsistency is a common cause of "why doesn’t my merge work?" bugs. One table has "HR", another has "hr", a third has "Human Resources". Always standardize case before comparing or merging on text columns. It’s a one-liner that saves hours of debugging.

3
Find Admin Employees (Case-Insensitive)

Find all employees in the Admin department, regardless of how the department name is capitalized.

Tables: worker

Flags per Video

Table: user_flags
user_firstnameuser_lastnamevideo_idflag_id
RichardHassony6120QOlsfU0cazx3
MarkMayCt6BUPvE2sM1cn76u
GinaKormandQw4w9WgXcQ1i43zk
MarkMayCt6BUPvE2sM1n0vef
MarkMayjNQXAC9IVRw1sv6ib
4
Flags per Video
View solution

For each video, find how many unique users flagged it. A unique user can be identified using the combination of their first name and last name. Do not consider rows in which there is no flag ID.

Tables: user_flags

Percentage of Shipable Orders

Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80
Table: customers
idfirst_namelast_namecityaddressphone_number
8JohnJosephSan Francisco928-386-8164
7JillMichaelAustin813-297-0692
4WilliamDanielDenver813-368-1200
5HenryJacksonMiami808-601-7513
13EmmaIsaacMiami808-690-5201
5
Find the percentage of shipable orders
View solution

Find the percentage of shipable orders. Consider an order is shipable if the customer's address is known.

Tables: orders, customers

First Names With Six Letters Ending in 'h'

Table: worker
worker_idfirst_namelast_namesalaryjoining_datedepartment
1MonikaArora1000002014-02-20HR
2NiharikaVerma800002014-06-11Admin
3VishalSinghal3000002014-02-20HR
4AmitahSingh5000002014-02-20Admin
5VivekBhati5000002014-06-11Admin
6
First Names With Six Letters Ending in 'h'
View solution

Find all workers whose first name contains 6 letters and also ends with the letter 'h'. Display all information about the workers in output.

Tables: worker

Key Takeaways

  • .str accessor gives you vectorized string operations on every element.
  • .str.strip() before merging — invisible whitespace breaks joins.
  • .str.upper() / .str.lower() for case-insensitive comparison.
  • + concatenates strings but returns NaN if any part is NaN.
  • Python uses 0-indexed positions (unlike SQL’s 1-indexed).

What’s Next

These methods handle 80% of string work. Next: splitting delimited strings, pattern matching with regex, and extracting structured data from messy text.