Split and Join Patterns

Concepts covered: Splitting & Joining

Calling split() and then join() covers a lot of text reformatting, and you'll see the pair constantly in production code.

Changing delimiters

Convert between different delimited formats:

# Convert comma-separated to tab-separated
csv_data = "name,age,city"
tsv_data = "\t".join(csv_data.split(","))
print(tsv_data)

# Convert path separators (Windows to Unix)
windows_path = "C:\\Users\\Maya\\Documents"
unix_path = "/".join(windows_path.split("\\"))
print(unix_path)

Output:

name	age	city
C/Users/Maya/Documents

Normalizing whitespace

Replace multiple spaces with single spaces:

messy = "too    many     spaces"

clean = " ".join(messy.split())
print(clean)

Output:

too many spaces

split() without arguments splits on any whitespace and removes empty strings. Joining with a single space normalizes the spacing.

Transforming each element

Process each part before joining back:

name = "maya johnson"

# Title case each word
title_name = " ".join(word.capitalize() for word in name.split())
print(title_name)

# Abbreviate to initials
initials = ".".join(word[0].upper() for word in name.split()) + "."
print(initials)

Output:

Maya Johnson
M.J.

The first returns the full name with each part capitalized, and the second returns the initials "M.J."

Now try filling in the blank to convert a snake_case variable name to Title Case using split and join.

Split on the input delimiter, apply a transformation to each piece, then rejoin with the output delimiter. Case conversion and abbreviation both fit that shape, as do most per-word changes, and the whole thing stays one readable expression.

String case conversions include lower(), upper(), capitalize() (first letter only), and title() (first letter of every word). For custom casing logic like snake_case to CamelCase, split-transform-join gives you full control over each word.

About this lesson

This section is part of the Strings: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.