Skip to main content

Programming fundamentals · Beginner · 18 min

Work with text: strings, f-strings and Unicode in Python

By Padding · Updated 18 Nov 2026
Python 3.15.2 · python:3.15-slim (Debian)

You will clean a messy list of names and phone numbers with Python's string methods, format it as an aligned table with f-strings, handle accented names and emoji correctly, and end with a script that writes a clean UTF-8 contact file.

On this page

Most data that reaches a program arrives as text: names typed into a form, numbers copied from a spreadsheet, lines in a file someone exported from their phone. That text is rarely tidy. It has stray spaces, random capitals, phone numbers written five different ways, and names with accents and tone marks that look identical on screen but are stored differently underneath.

In Python, text is a value of type str, a string. In this tutorial you take a messy contact list and clean it one problem at a time: spaces and case, splitting lines into fields, phone numbers, then an aligned table with f-strings. After that you look at Unicode, the standard that gives every character in every writing system a number, and see why len() and == can surprise you with Yoruba, Vietnamese and French names or an emoji. You finish with one script that does the whole clean-up and saves the result as UTF-8.

The examples use python3. On Windows outside WSL, type py instead. Every step runs offline once Python is installed, so on prepaid mobile data it costs nothing to work through.

Step 1 · Create a messy contact list

Make a folder for this tutorial and write a small script that creates the messy list. A script is used here rather than typing the file by hand because one line needs invisible characters that are hard to type and easy to lose when text is copied, as you will see:

Bash
mkdir -p ~/strings-demo
cd ~/strings-demo
cat > make_contacts.py <<'PY'
from pathlib import Path

lines = [
    "# name, phone, paid",
    "  ADÉBÁYỌ̀ ogunlesi , 0803 555 0142 , 125000",
    "nguyễn thị hoa,+84 90-123-4567,2500000",
    "",
    "zoë DURAND ,  +33 6 12 34 56 78, 48000",
    "chidi okafor 🎉, (0812) 555-0199 , 1250000",
    "ade\u0301ba\u0301yo\u0323\u0300 OGUNLESI,08035550142,125000",
]

Path("contacts.txt").write_text("\n".join(lines) + "\n", encoding="utf-8")
print("wrote", len(lines), "lines")
PY
python3 make_contacts.py
cat contacts.txt

Line by line:

  • from pathlib import Path brings in Path, which represents a file on disk.
  • lines = [...] is a list of strings, one per line of the file. Each line is a name, a phone number and an amount paid into a savings group, separated by commas, in whatever currency you use.
  • The first line starts with #: it is a comment for humans. The fourth is empty. Real files have both.
  • The last line spells the first person's name a second way, using escapes. Inside a Python string, a backslash, u and four hexadecimal digits stand for one character by its Unicode number, so \u0301 is a combining acute accent that sits on the letter before it, \u0323 is a combining dot below and \u0300 is a combining grave accent. Writing them as escapes keeps them visible in the code and stops an editor or a copy and paste from quietly merging them into the letters. You come back to this in a later step.
  • "\n".join(lines) glues the list into one string with a newline between items, and write_text(..., encoding="utf-8") saves it.

Check it worked

The script prints wrote 7 lines, and cat shows the file: a comment line, five contacts and a blank line. The first and last contacts look like the same person, Adébáyọ̀ Ogunlesi, with different spacing and capitals.

Step 2 · Fix spaces and capital letters

Strings come with built-in methods: functions attached to the value, called with a dot. Try the ones that fix spacing and case on the first name:

Bash
cat > case.py <<'PY'
raw = "  ADÉBÁYỌ̀ ogunlesi "

print(repr(raw))
print(repr(raw.strip()))
print(raw.strip().lower())
print(raw.strip().upper())
print(raw.strip().title())
print("mcdonald o'brien".title())
print("Straße".lower(), "Straße".casefold())
PY
python3 case.py
  • repr(raw) shows the string with its quotes, so you can see the spaces at each end.
  • strip() returns a copy with spaces, tabs and newlines removed from both ends. lstrip() and rstrip() do one end only.
  • lower(), upper() and title() change case. title() capitalises the first letter of every word. Methods can be chained: raw.strip().title() strips first, then capitalises the result.
  • casefold() is a stronger lower() meant for comparing text. The German ß becomes ss, so "STRASSE" and "Straße" match after casefolding.

None of these change raw itself. Strings in Python are immutable: every method returns a new string and leaves the original alone. To keep a result, assign it, as in name = raw.strip().

Check it worked

The output is:

Text
'  ADÉBÁYỌ̀ ogunlesi '
'ADÉBÁYỌ̀ ogunlesi'
adébáyọ̀ ogunlesi
ADÉBÁYỌ̀ OGUNLESI
Adébáyọ̀ Ogunlesi
Mcdonald O'Brien
straße strasse

The accents and the tone marks change case with their letters. Notice Mcdonald: title() knows nothing about names, so it cannot produce McDonald. For a contact list that is usually good enough, but never run title() over names people have already written carefully.

Step 3 · Split each line into fields

Each contact line holds three values separated by commas. split(",") cuts a string at every comma and returns a list of the pieces. Read the file and split every line:

Bash
cat > fields.py <<'PY'
from pathlib import Path

text = Path("contacts.txt").read_text(encoding="utf-8")

for line in text.splitlines():
    if not line.strip() or line.startswith("#"):
        continue
    parts = line.split(",")
    fields = [part.strip() for part in parts]
    print(len(fields), " | ".join(fields))
PY
python3 fields.py
  • read_text(encoding="utf-8") reads the whole file into one string.
  • splitlines() splits that string into a list of lines, without the newline characters.
  • not line.strip() is true for a line that is empty or only spaces, because an empty string counts as false. line.startswith("#") is true for comment lines. continue skips to the next line.
  • [part.strip() for part in parts] is a list comprehension: it builds a new list by running strip() on every piece.
  • " | ".join(fields) is the reverse of split: it joins a list of strings into one, with " | " between them. The separator goes before the dot, which reads oddly at first.

Check it worked

Five lines print, each starting with 3, which means every contact split into exactly three fields. The first is 3 ADÉBÁYỌ̀ ogunlesi | 0803 555 0142 | 125000, with the spaces around each value gone. The comment and the blank line do not appear.

Step 4 · Clean the phone numbers

The numbers mix spaces, dashes and brackets, and the Nigerian ones are written in local form starting with 0. Write a function that removes the punctuation with replace and turns a leading 0 into the +234 country code:

Bash
cat > phones.py <<'PY'
def clean_phone(raw):
    phone = raw.strip()
    for junk in (" ", "-", "(", ")"):
        phone = phone.replace(junk, "")
    if phone.startswith("0"):
        phone = "+234" + phone[1:]
    return phone


for raw in ["0803 555 0142", "+84 90-123-4567", "(0812) 555-0199"]:
    phone = clean_phone(raw)
    print(phone, phone[:4], phone[-4:], phone[:4] + "..." + phone[-4:])
PY
python3 phones.py
  • phone.replace(junk, "") returns a copy with every junk character replaced by nothing. The loop runs it for a space, a dash and both brackets.
  • phone.startswith("0") checks the first character. The +234 rule is specific to this list, where every local number is Nigerian; change it to your own country code.
  • phone[1:] is a slice: the characters from position 1 to the end, which drops the leading 0. Positions count from 0.
  • phone[:4] is the first four characters and phone[-4:] the last four. Negative positions count back from the end. Combined, they make a masked number that is safe to show on screen.

Check it worked

The output is:

Text
+2348035550142 +234 0142 +234...0142
+84901234567 +849 4567 +849...4567
+2348125550199 +234 0199 +234...0199

The Vietnamese number already had its +84 code, so only its spaces and dashes were removed.

Step 5 · Format a table with f-strings

An f-string is a string with an f before the quote. Anything inside {} is run as Python and its value is inserted. After a colon you can add a format: a width, an alignment and how to show numbers. Print three cleaned contacts as a table:

Bash
cat > table.py <<'PY'
rows = [
    ("Adébáyọ̀ Ogunlesi", "+2348035550142", 125000),
    ("Nguyễn Thị Hoa", "+84901234567", 2500000),
    ("Zoë Durand", "+33612345678", 48000),
]

print(f"{'Name':<20}{'Phone':<16}{'Paid':>12}")
print("-" * 48)
for name, phone, paid in rows:
    print(f"{name:<20}{phone:<16}{paid:>12,}")

total = sum(paid for _, _, paid in rows)
print(f"{'Total':<36}{total:>12,}")
print(f"{total / len(rows):,.2f} average")
print(f"{0.0823:.1%} fee")
PY
python3 table.py
  • {name:<20} pads the name with spaces to 20 characters, aligned left. > aligns right and ^ centres.
  • {paid:>12,} right-aligns the number in 12 characters and adds a comma every three digits.
  • "-" * 48 repeats a string: 48 dashes.
  • {total / len(rows):,.2f} shows the average with thousands separators and exactly two decimal places.
  • {0.0823:.1%} multiplies by 100 and adds a percent sign, with one decimal place.

Check it worked

The amounts line up on the right as 125,000, 2,500,000 and 48,000, the total line reads 2,673,000, and the last two lines are 891,000.00 average and 8.2% fee.

Look closely at the first row: its phone number starts one column further left than the others. The width counted 20 characters, but one of them in Adébáyọ̀ is a tone mark that takes no space on screen. The next step explains why.

Step 6 · See why len() surprises you with Unicode

Every character is stored as one or more Unicode code points, numbered like U+0041 for A. Some accented letters exist as one code point and also as a plain letter followed by combining marks. They look the same, but Python compares code points. Compare the two spellings of the name:

Bash
cat > unicode_len.py <<'PY'
import unicodedata

typed = "Adébáyọ̀"
pasted = "Ade\u0301ba\u0301yo\u0323\u0300"

print(typed, pasted)
print(len(typed), len(pasted), typed == pasted)

for char in pasted:
    print(f"U+{ord(char):04X} {unicodedata.name(char)}")

nfc = unicodedata.normalize("NFC", pasted)
print(len(nfc), nfc == typed)
print(pasted.title(), nfc.title())

print(len("Hoa"), len("Nguyễn"), len("🎉"), len("👍🏾"))
PY
python3 unicode_len.py
  • typed uses ready-made accented letters. pasted is built with \u escapes, as in the contact list, from plain letters plus separate combining marks, so the two strings really are stored differently even though they look the same.
  • len() counts code points, not the letters you see.
  • ord(char) gives a character's code point as a number, and :04X prints it as four hexadecimal digits. unicodedata.name() gives its official name.
  • unicodedata.normalize("NFC", ...) combines letters and marks into single code points wherever Unicode has one. "NFD" does the opposite and splits them apart.

Check it worked

The two names print identically, but the next line is 8 11 False: same name, different lengths, not equal. The listing shows pasted as plain letters followed by COMBINING ACUTE ACCENT, COMBINING DOT BELOW and COMBINING GRAVE ACCENT. After normalising, the line reads 8 True.

Two more results matter. pasted.title() gives AdéBáYọ̀, with capitals in the middle, because title() treats a combining mark as the end of a word. Normalise before you change case. And the last line is 3 6 1 2: 🎉 is one code point, but the thumbs up with a skin tone is two, an emoji plus a modifier. Even after normalising, typed is 8 code points for 7 visible letters, because Yoruba ọ̀ has no single code point and keeps its grave accent as a separate mark. That is why the table was off by one.

Names typed on different phone keyboards, copied from PDFs or taken from file names can arrive in either form. Normalise text to NFC as soon as you read it, then compare, count and change case.

Step 7 · Read and write files as UTF-8

On disk, text is bytes. An encoding is the rule that turns characters into bytes and back, and UTF-8 is the one almost everything uses today. See what happens when the rule on reading does not match the rule on writing:

Bash
cat > encodings.py <<'PY'
from pathlib import Path

name = "Zoë Nguyễn"
data = name.encode("utf-8")
print(len(name), "characters,", len(data), "bytes")
print(data)

print(data.decode("utf-8"))
print(data.decode("cp1252"))

Path("name.txt").write_text(name + "\n", encoding="utf-8")
print(Path("name.txt").read_text(encoding="utf-8"), end="")

try:
    data.decode("ascii")
except UnicodeDecodeError as err:
    print("ascii failed:", err.reason)
PY
python3 encodings.py
  • encode("utf-8") turns a string into bytes. Python prints bytes as b'...', with anything outside plain English letters shown as \x codes.
  • decode("utf-8") turns the bytes back into the same string.
  • decode("cp1252") reads the same bytes with an old Windows encoding. It does not fail; it gives the wrong characters, which is worse.
  • decode("ascii") raises UnicodeDecodeError, because ASCII only covers 128 characters. try and except catch the error so the script carries on.

Check it worked

The first line is 10 characters, 13 bytes: ë takes two bytes in UTF-8 and ễ takes three. The UTF-8 decode gives back Zoë Nguyễn, the cp1252 decode prints Zoë Nguyá»…n, and the last line is ascii failed: ordinal not in range(128).

That ë pattern is called mojibake. When you see it in a spreadsheet or a web page, a file was written in UTF-8 and read with a different encoding. Python 3.15 uses UTF-8 by default when you leave encoding out, but earlier versions used the computer's own setting, which on many Windows machines is not UTF-8. Write encoding="utf-8" every time so the same script behaves the same on every version and every computer.

Step 8 · Write the cleaning script

Put every step together. The script reads contacts.txt, normalises and tidies each name, cleans each phone number, drops duplicates, saves a clean file and prints an aligned table:

Bash
cat > clean_contacts.py <<'PY'
import unicodedata
from pathlib import Path

SOURCE = Path("contacts.txt")
TARGET = Path("contacts_clean.txt")


def clean_name(raw):
    name = unicodedata.normalize("NFC", raw)
    name = " ".join(name.split())
    return name.title()


def clean_phone(raw):
    phone = raw.strip()
    for junk in (" ", "-", "(", ")"):
        phone = phone.replace(junk, "")
    if phone.startswith("0"):
        phone = "+234" + phone[1:]
    return phone


def width(text):
    total = 0
    for char in text:
        if unicodedata.combining(char):
            continue
        if unicodedata.east_asian_width(char) in ("W", "F"):
            total += 2
        else:
            total += 1
    return total


def pad(text, size):
    return text + " " * (size - width(text))


contacts = {}
for line in SOURCE.read_text(encoding="utf-8").splitlines():
    if not line.strip() or line.startswith("#"):
        continue
    raw_name, raw_phone, raw_paid = line.split(",")
    name = clean_name(raw_name)
    key = name.casefold()
    if key in contacts:
        print("skipped duplicate:", name)
        continue
    contacts[key] = (name, clean_phone(raw_phone), int(raw_paid))

rows = sorted(contacts.values())
TARGET.write_text(
    "".join(f"{name},{phone},{paid}\n" for name, phone, paid in rows),
    encoding="utf-8",
)

print(f"{pad('Name', 22)}{'Phone':<16}{'Paid':>12}")
print("-" * 50)
for name, phone, paid in rows:
    print(f"{pad(name, 22)}{phone:<16}{paid:>12,}")
total = sum(paid for _, _, paid in rows)
print(f"{pad('Total', 38)}{total:>12,}")
print(f"wrote {len(rows)} contacts to {TARGET}")
PY
python3 clean_contacts.py
cat contacts_clean.txt

What is new here:

  • clean_name normalises to NFC first, so title() works and both spellings of Adébáyọ̀ become the same string. " ".join(name.split()) is a common idiom: split() with no argument splits on any run of spaces and drops the ends, and join puts single spaces back.
  • raw_name, raw_phone, raw_paid = line.split(",") unpacks the three fields into three names. A line with more or fewer commas raises a ValueError here, which is what you want to hear about.
  • contacts is a dictionary keyed by the casefolded name, so a name seen twice is skipped whatever its capitals. int(raw_paid) turns the amount into a number; int() ignores spaces around digits.
  • width estimates how many columns a string takes in a terminal: combining marks take none, and characters Unicode marks as wide (W) or full-width (F), which includes most emoji and Chinese, Japanese and Korean characters, take two. pad uses it in place of :<22, which counts code points.
  • sorted(contacts.values()) sorts the rows by name, then the file is written with one f-string per contact.

Check it worked

The script prints skipped duplicate: Adébáyọ̀ Ogunlesi, then a table with four contacts sorted by name, the total 3,923,000 and wrote 4 contacts to contacts_clean.txt. In most terminals the phone column now lines up on every row, including the rows with tone marks and the emoji. The cat output is the clean file:

Text
Adébáyọ̀ Ogunlesi,+2348035550142,125000
Chidi Okafor 🎉,+2348125550199,1250000
Nguyễn Thị Hoa,+84901234567,2500000
Zoë Durand,+33612345678,48000

The width estimate is close, not perfect: a few emoji sequences, such as flags, still draw differently from terminal to terminal.

When you have finished, delete the demo folder:

Bash
cd ~
rm -rf ~/strings-demo

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.