Lesson 08
String Methods — Practice Programs
Build real programs by combining split(), join(), strip(), case conversion, and validation methods. Focus on method chaining and practical data cleaning.
What you'll learn
| Topic | Focus |
|---|---|
| Method chaining | Combine multiple string methods |
| Data parsing | Dates, delimited lists |
| Validation | Gate input before processing |
| Module review | 15+ essential string methods |
Learning roadmap
Every practical string program follows this workflow:
- Receive raw text
- Clean unwanted spaces
- Validate the content
- Split or transform data
- Produce formatted output
Program 1: Parse a Date (DD-MM-YYYY)
Goal: Extract the day, month, and year from a date string.
s = "08-07-2023"
parts = s.split("-")
print("Day =", parts[0])
print("Month =", parts[1])
print("Year =", parts[2])Expected output:
Day = 08
Month = 07
Year = 2023Memory:
split("-")converts one date string into a 3-item list.
Program 2: Split Fruit List & Rejoin
Problem: A string contains two different delimiters.
s = "Apple#Mango-Kiwi#Guava"
x = s.split("#")
y = x[1].split("-")
x[1] = y[0]
x.insert(-1, y[1])
print(x)Expected output:
['Apple', 'Mango', 'Kiwi', 'Guava']Pattern: Split the outer delimiter first, then split the inner delimiter.
Program 3: Build a Sentence from a List
join() combines list items into one string.
words = ["PYTHON", "IS", "AN", "FUN", "PROG", "LANG"]
sentence = " ".join(words)
print(sentence)Expected output:
PYTHON IS AN FUN PROG LANGRemember: The string before
.join()becomes the separator.
Program 4: Validate an Alphanumeric ID
Goal: Accept only letters and digits.
s = "Python312"
print(s.isalnum()) # True
s = "Python 312"
print(s.isalnum()) # False
s = "Python3.12"
print(s.isalnum()) # False| Character | Allowed? |
|---|---|
| Letters | Yes |
| Digits | Yes |
| Spaces | No |
| Symbols | No |
Dot (.) |
No |
Program 5: Clean & Format a Name
Problem: User enters extra spaces.
s = "Nirkar Behra "
s = s.strip()
formatted = " ".join(s.split())
print(formatted)Expected output:
Nirkar BehraMethod chain explained:
Method cheat sheet
| Task | Method |
|---|---|
| Capitalize first letter | capitalize() |
| Title Case | title() |
| Upper / Lower | upper() / lower() |
| Swap letter case | swapcase() |
| Find position | index() |
| Count occurrences | count() |
| Remove edge spaces | strip() |
| String → List | split() |
| List → String | join() |
| Check beginning | startswith() |
| Check ending | endswith() |
| Letters & digits only | isalnum() |
Method chaining (professional style)
Instead of writing multiple lines, Python lets you chain methods.
Example 1:
raw = " PYTHON310 "
cleaned = raw.strip().lower()
print(cleaned)
# python310Example 2:
name = " nirkar behra "
result = " ".join(name.split()).title()
print(result)
# Nirkar BehraChaining makes code shorter and easier to read.
Mini challenge (try yourself)
Input:
john doe 123Requirements:
- Remove leading/trailing spaces
- Convert to title case
- Keep only single spaces
- Check whether the final string is alphanumeric
Try solving it before viewing the answer.
Expected final text:
John Doe 123Validation: isalnum() → False (because spaces are present)
Summary mind map
Everything you've learned:
| Category | Methods |
|---|---|
| Case | upper(), lower(), title(), capitalize(), swapcase() |
| Validation | isalpha(), isdigit(), isalnum(), isupper() |
| Search | index(), count(), startswith(), endswith() |
| Cleaning | strip(), lstrip(), rstrip() |
| Conversion | split(), join() |
Playground
Copy and run this combined example.
dob = "08-07-2023"
parts = dob.split("-")
print("Day =", parts[0], "Month =", parts[1], "Year =", parts[2])
words = ["PYTHON", "IS", "AN", "FUN", "PROG", "LANG"]
print(" ".join(words))
name = "Nirkar Behra "
print(" ".join(name.strip().split()))Use the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
- Extract day, month, year from
"06-05-2024":
s = "06-05-2024"
parts = s.split("-")Answer: Day → parts[0] · Month → parts[1] · Year → parts[2]
-
Which method accepts only letters and digits?
Answer:isalnum() -
Rebuild a sentence from a list:
words = ["Python", "is", "an", "oop", "lang"]
sentence = " ".join(words)Answer: "Python is an oop lang"
Module complete
You now know 15+ essential string methods and how to combine them in practical programs.
Interview shortcut: Clean with strip() + split() + join() · Validate with isalnum() · Parse with split() · Chain methods left to right.
Next module: Mutable and Immutable Objects — learn why strings behave differently from lists and dictionaries.
Loading playground…