Tutorial

Python String Assignments: 5 Real-World Slicing and Formatting Drills

Practice Python strings on real tasks — email slicer, reverser, cleaner, vowel counter, and f-string formatter with solutions.

Anuj SainiAug 23, 20264 min read

Five assignments that feel like real tickets — slice an email, reverse a string, clean inputs, count vowels, and format output — each with a one-line Python solution.

What 5 tickets are inside?

Q1 Email slicer -> Q2 Reverser -> Q3 Cleaner -> Q4 Vowel counter -> Q5 F-string formatter, each with input variable pre-set (email = "student.name@university.edu", sentence = "Python Programming" etc.) so you only write the verb. Do Python basics workbook if if/else still slows you.

Ingredients: 5 prompts; no libraries; solutions are one to two lines each.

How do you slice, clean, and count strings?

python
# Q1: Email slicer — domain after @
email = "student.name@university.edu"
domain = email.split("@")[1]
print(domain)  # university.edu
# robust: email.strip().lower().split("@")[1] if "@" in email else ""
 
# Q2: Reverser — slice step -1
sentence = "Python Programming"
print(sentence[::-1])  # gnimmargorP nohtyP
 
# Q3: Cleaner — strip + title case
raw_input = "    pyTHoN is aWeSoME    "
print(raw_input.strip().title())  # Python Is Awesome
print(raw_input.strip().lower())  # python is awesome — alternative spec
 
# Q4: Vowel counter — lowercase then count 'a'
text_block = "Data science involves extracting actionable insights from raw data."
print(text_block.lower().count("a"))  # 8
# all vowels:
print(sum(text_block.lower().count(v) for v in "aeiou"))  # 23
print(len(text_block))  # 68 chars incl spaces
 
# Q5: F-string formatter — exact spec required
last_name = "Bond"
first_name = "James"
license_code = 7
print(f"Agent {last_name}, {first_name} {last_name}. License: {license_code:03d}.")
# -> Agent Bond, James Bond. License: 007.

Rendered output line-for-line: university.edu, reversed sentence, Python Is Awesome, 8 (a-count), Agent Bond, James Bond. License: 007. — the autograder checks exact strings, so whitespace and padding matter.

Feature / Criteria

Gotcha: Forgetting strip() Before split() on Emails

" user@domain.com ".split("@")[1] yields "domain.com " with a trailing space that fails equality checks. Always strip() before split() on user input — the notebook shows a hidden test failing until the strip is added.

How do you retain this after submission?

Replay each Q blind until the one-liner is immediate. Then string methods reappear as df['col'].str.strip()/str.contains in Pandas fundamentals and as re.sub in Python fundamentals master.


Download the Notebook and Practise

This article is a walkthrough of a runnable Jupyter notebook. Download the original .ipynb and run it locally or on Colab — every code block above appears in order.

Download the String Assignments Notebook

Get the complete .ipynb with outputs — runs on any Python 3.10+ environment with pandas, numpy, and the libraries listed in setup.

Download .ipynb

Continue your track: Data Analyst Roadmap · Python and Pandas Guide · SQL NULL Handbook · SQL JOIN Fan-Out · Topfolio Practice · Data Analyst vs Engineer

Dataset generators where applicable are in courses/workbooks/generators/ — see citations atop for the exact *.py source for this notebook.


Frequently Asked Questions

How do you extract a domain from an email in Python?

email.split('@')[1] — split on '@' and take index 1. Wrap with .strip().lower() to normalise.

How do you reverse a string?

sentence[::-1] — slice with step -1. No loop needed.

What is the difference between strip, lstrip, and rstrip?

strip removes both ends, lstrip left only, rstrip right only. Default removes whitespace; pass chars to strip specific characters.

How do you count vowels correctly?

Normalise case first: text.lower().count('a') counts only 'a'. For all vowels sum(text.lower().count(v) for v in 'aeiou').

Frequently Asked Questions

How do you extract a domain from an email in Python?

email.split('@')[1] — split on '@' and take index 1. Wrap with .strip().lower() to normalise.

How do you reverse a string?

sentence[::-1] — slice with step -1. No loop needed.

What is the difference between strip, lstrip, and rstrip?

strip removes both ends, lstrip left only, rstrip right only. Default removes whitespace; pass chars to strip specific characters.

How do you count vowels correctly?

Normalise case first: text.lower().count('a') counts only 'a'. For all vowels sum(text.lower().count(v) for v in 'aeiou').

Anuj Saini

Written by

Anuj SainiFounder & Lead Instructor

Founder at Topfolio with 6+ years in data & analytics across JPMC, Ultrahuman, and high-growth startups. Sat on hiring panels, reviewed 500+ resumes, and writes practical SQL & data guides.