Working with Text in Python: Cleaning, Splitting and Formatting

Python for Beginners: Learn to Code with Real Projects · Functions, Text and Files · 5 min read · Updated

Clean text step by step

  1. Real text is often messy, with extra spaces, capital letters and odd separators.
  2. Cleaning methods such as strip and lower give you a tidy, consistent text.
  3. Split cuts text into pieces, and a filter such as isdigit keeps only the characters you need.
  4. Now the data is ready to use, store or compare.

Much real-world data is text: names, addresses, emails, product codes, lines of a file. It is also messy: extra spaces, mixed capital letters, odd separators. Cleaning and shaping text is one of the most common jobs in practical Python. In this lesson you will learn the tools for it and use them on small, realistic examples.

Text is a sequence

A string is a sequence of characters, and like a list you can use an index and a slice. title[0] is the first character. title[-1] is the last. title[1:4] takes characters from index 1 up to but not including index 4. title[::-1] reverses the text. Strings cannot be changed in place. Methods such as .upper() return a new string, so you must store the result.

raw = "  +91 98765-43210  "
digits = "".join(ch for ch in raw if ch.isdigit())
print(digits)

line = "Asha, Pune, 29"
name, city, age = [part.strip() for part in line.split(",")]
print(name, city, int(age))

words = "python is fun".split()
print(words, " ".join(w.capitalize() for w in words))

s = "Resume_Final_v2.PDF"
print(s.lower(), s.replace("_", " "), s.endswith(".pdf"))
print(s.lower().endswith(".pdf"))
print("final" in s.lower())

title = "Python"
print(title[0], title[-1], title[1:4], title[::-1])

print(f"{'Item':<10}{'Price':>8}")
print(f"{'Pen':<10}{12.5:>8.2f}")
print(f"{'Notebook':<10}{120:>8.2f}")

email = "Asha@Example.COM "
print(email.strip().lower())
print("@" in email, email.split("@")[1].strip().lower())
919876543210
Asha Pune 29
['python', 'is', 'fun'] Python Is Fun
resume_final_v2.pdf Resume Final v2.PDF False
True
True
P n yth nohtyP
Item         Price
Pen          12.50
Notebook    120.00
asha@example.com
True example.com

What each part does

  • Cleaning a phone number: the program keeps only the digits with ch.isdigit() and joins them with "".join(...). A generator inside join is a compact loop. The result 919876543210 includes the country code, which is correct for the input.
  • Splitting: line.split(",") cuts text at each comma and gives a list. We then strip() each part. .split() without an argument splits on any spaces.
  • Joining: " ".join(words) is the opposite. It glues a list of texts together with the separator in front of .join.
  • Changing: .lower(), .replace("_", " "), .capitalize(). Remember that each returns a new text.
  • Checking: .endswith(".pdf") is False for Resume_Final_v2.PDF because of the capital letters. Lower the text first, and then it is True. The in operator checks for a piece of text inside text.
  • Aligning: in f"{'Item':<10}{'Price':>8}" the <10 means left-align in 10 characters and >8.2f means right-align a decimal with two places in 8 characters. This makes neat tables.

More methods worth knowing

  • .startswith(...), .endswith(...), .find(...) and .count(...).
  • .isdigit(), .isalpha(), .isspace() to check what a text contains.
  • .title() capitalises each word, but it makes mistakes with names like McDonald, and with apostrophes such as o'neil. For real names, be careful.
  • .splitlines() splits text at line ends, useful with files.
  • .zfill(5) pads a number text with zeros: "42".zfill(5) is 00042.

Cleaning an email address

The last lines of the program clean an email: strip() removes the spaces, lower() makes it consistent, and .split("@")[1] takes the part after the at sign, the domain. Notice that "@" in email is a basic check only. Checking whether an email is truly valid is harder than it looks. For real forms, check for a simple pattern, and confirm by sending a message with a link.

Escape characters and multi-line text

A backslash in text starts a special character. \n is a new line, \t is a tab, and \\ is one backslash. This matters for Windows file paths: write them with a leading r, as in r"C:\Users\Asha", so Python does not treat the backslashes as escapes. Triple quotes make text that spans several lines.

Unicode and languages

Python 3 handles text in all languages. You can store names in Hindi, Arabic, French or Chinese in a string with no special steps. When you read or write files, always give encoding="utf-8", as you will do in the next lesson. Without it, Windows may use a different encoding and break non-English letters.

Your task

  1. Write clean_phone(text) that returns only the digits of a phone number. Test it with "(022) 2345-6789" and "+44 20 7946 0958".
  2. Take the text " Asha Verma , Pune , 29 ", split it and print name, city and age without extra spaces.
  3. Print a small price list as an aligned table with a header, using < and > in f-strings.
  4. Write file_kind(name) that returns "pdf" for names ending in .pdf in any letter case, and "other" otherwise.

How to check it worked

  • clean_phone("(022) 2345-6789") returns 02223456789.
  • Your aligned table has the columns lined up.
  • file_kind("CV.PDF") returns pdf.

Frequently asked questions

Why does .upper() not change my variable?

Strings cannot be changed in place. Methods return a new string, so store it: name = name.upper().

How do I split text on commas?

Use text.split(","), which gives a list of parts. Then clean each part with strip().