Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner20 min

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

Raw → Clean → Validate → Split → Output workflow

Every practical string program follows this workflow:

  1. Receive raw text
  2. Clean unwanted spaces
  3. Validate the content
  4. Split or transform data
  5. Produce formatted output

Program 1: Parse a Date (DD-MM-YYYY)

Goal: Extract the day, month, and year from a date string.

Original date string 08-07-2023
Split by dash into day, month, year
python
s = "08-07-2023"

parts = s.split("-")

print("Day =", parts[0])
print("Month =", parts[1])
print("Year =", parts[2])

Expected output:

text
Day = 08
Month = 07
Year = 2023

Memory: split("-") converts one date string into a 3-item list.

Program 2: Split Fruit List & Rejoin

Problem: A string contains two different delimiters.

Original Apple#Mango-Kiwi#Guava
First split on #
Second split on - inside Mango-Kiwi
python
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:

text
['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.

List chips joined with space into sentence
python
words = ["PYTHON", "IS", "AN", "FUN", "PROG", "LANG"]

sentence = " ".join(words)

print(sentence)

Expected output:

text
PYTHON IS AN FUN PROG LANG

Remember: The string before .join() becomes the separator.

Program 4: Validate an Alphanumeric ID

Goal: Accept only letters and digits.

Python312 valid, Python 312 and Python3.12 invalid
python
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.

Name cleaning: messy → trimmed → single spaces
python
s = "Nirkar   Behra    "

s = s.strip()

formatted = " ".join(s.split())

print(formatted)

Expected output:

text
Nirkar Behra

Method chain explained:

strip → split → join pipeline

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.

Raw → strip → lower → Result

Example 1:

python
raw = "   PYTHON310   "

cleaned = raw.strip().lower()

print(cleaned)
# python310

Example 2:

python
name = "  nirkar   behra "

result = " ".join(name.split()).title()

print(result)
# Nirkar Behra

Chaining makes code shorter and easier to read.

Mini challenge (try yourself)

Input:

text
  john    doe 123

Requirements:

  • 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:

text
John Doe 123

Validation: isalnum() → False (because spaces are present)

Summary mind map

String Methods hub — Clean, Validate, Search, Convert, Split/Join

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.

python
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

  1. Extract day, month, year from "06-05-2024":
python
s = "06-05-2024"
parts = s.split("-")

Answer: Day → parts[0] · Month → parts[1] · Year → parts[2]

  1. Which method accepts only letters and digits?
    Answer: isalnum()

  2. Rebuild a sentence from a list:

python
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…