String Methods
Progress Tracking
Log in to save this lesson and continue from where you left off.
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:
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
# Simple concatenation with +
worker["full_name"] = (
worker["first_name"] + " " + worker["last_name"]
)
# With formatting
worker["label"] = (
"Employee: " + worker["first_name"]
+ " (" + worker["department"] + ")"
)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:
# Clean before merging
df["name"] = df["name"].str.strip()Combining Cleaned Text
Create a clean full name by stripping whitespace and uppercasing. The starter strips — add the concatenation.
Substring Extraction
# 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)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
For each employee, extract the first 3 characters of their first name and calculate name length. Sort by length descending.
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.
Find all employees in the Admin department, regardless of how the department name is capitalized.
Flags per Video
| user_firstname | user_lastname | video_id | flag_id |
|---|---|---|---|
| Richard | Hasson | y6120QOlsfU | 0cazx3 |
| Mark | May | Ct6BUPvE2sM | 1cn76u |
| Gina | Korman | dQw4w9WgXcQ | 1i43zk |
| Mark | May | Ct6BUPvE2sM | 1n0vef |
| Mark | May | jNQXAC9IVRw | 1sv6ib |
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.
Percentage of Shipable Orders
| id | cust_id | order_date | order_details | total_order_cost |
|---|---|---|---|---|
| 1 | 3 | 2019-03-04 | Coat | 100 |
| 2 | 3 | 2019-03-01 | Shoes | 80 |
| 3 | 3 | 2019-03-07 | Skirt | 30 |
| 4 | 7 | 2019-02-01 | Coat | 25 |
| 5 | 7 | 2019-03-10 | Shoes | 80 |
| id | first_name | last_name | city | address | phone_number |
|---|---|---|---|---|---|
| 8 | John | Joseph | San Francisco | 928-386-8164 | |
| 7 | Jill | Michael | Austin | 813-297-0692 | |
| 4 | William | Daniel | Denver | 813-368-1200 | |
| 5 | Henry | Jackson | Miami | 808-601-7513 | |
| 13 | Emma | Isaac | Miami | 808-690-5201 |
Find the percentage of shipable orders. Consider an order is shipable if the customer's address is known.
First Names With Six Letters Ending in 'h'
| worker_id | first_name | last_name | salary | joining_date | department |
|---|---|---|---|---|---|
| 1 | Monika | Arora | 100000 | 2014-02-20 | HR |
| 2 | Niharika | Verma | 80000 | 2014-06-11 | Admin |
| 3 | Vishal | Singhal | 300000 | 2014-02-20 | HR |
| 4 | Amitah | Singh | 500000 | 2014-02-20 | Admin |
| 5 | Vivek | Bhati | 500000 | 2014-06-11 | Admin |
Find all workers whose first name contains 6 letters and also ends with the letter 'h'. Display all information about the workers in output.
Key Takeaways
.straccessor 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.