Skip to content

RESOURCES / BLOG

How Do I Use split() to Break Up Strings in Python?

Hey all,

I’m working on a Python project where I need to process a bunch of image filenames and extract things like the image type or tag from the name. I’ve seen people use something like split() on strings, but I’m not exactly sure how it works.

Can someone explain how to use Python to split strings properly, with a few examples? Thanks!

Great question! If you’re dealing with filenames, tags, URLs, CSV data, or user input, you’ll find yourself using Python’s split() function all the time.

The split() method is a built-in string method that lets you break a string into a list, based on a delimiter (a character or sequence that marks where to split).

string.split(separator, maxsplit)

  • separator. The delimiter (optional; default is whitespace).
  • maxsplit. Maximum number of splits (optional).
text = "sunset beach clouds"

words = text.split()

print(words)

# prints ['sunset', 'beach', 'clouds']Code language: PHP (php)

When no separator is given, Python splits on any whitespace (spaces, tabs, or newlines).

Let’s say you’re working with filenames like:

filename = "landscape_2024_highres.jpg"

You can split this by the underscore (_):

parts = filename.split("_")

print(parts)

# Outputs ['landscape', '2024', 'highres.jpg']Code language: PHP (php)

Want to extract just the year? Splice the list like this:

year = parts[1]

print(year)  # 2024Code language: PHP (php)
url = "https://res.cloudinary.com/demo/image/upload/v1650/sample.jpg"

parts = url.split("/")

print(parts)

# Outputs ['https:', '', 'res.cloudinary.com', 'demo', 'image', 'upload', 'v1650', 'sample.jpg']Code language: PHP (php)

Want just the public ID?

public_id = parts[-1].split(".")[0]

print(public_id)  # sampleCode language: PHP (php)

If you read data from a CSV file, you’ll want to split by commas:

row = "image1.jpg,portrait,1080"

columns = row.split(",")

print(columns)

# Outputs['image1.jpg', 'portrait', '1080']Code language: PHP (php)

You now have filename, tag, and resolution in separate variables.

text = "cloudinary image optimizer tool"

result = text.split(" ", 1)

print(result)

# Outputs ['cloudinary', 'image optimizer tool']Code language: PHP (php)

This splits only once, making it great when you only care about the first part.

Sometimes you want to split starting from the end:

path = "folder/image/vacation_2023.jpg"

parts = path.rsplit("/", 1)

print(parts)

# Outputs ['folder/image', 'vacation_2023.jpg']Code language: PHP (php)

Useful if you want to isolate a filename from its full path.

Let’s say you want to extract just the extension from an image file:

filename = "mountain_view.jpg"

extension = filename.split(".")[1]

print(extension) # prints “jpg”Code language: PHP (php)

Or safely handle filenames with multiple dots:

filename = "portfolio.final.2025.jpg"

# We set the maxsplit argument to 1

parts = filename.rsplit(".", 1print(parts)

# Outputs ['portfolio.final.2025', 'jpg']Code language: PHP (php)

This avoids breaking too early and ensures you get the file extension.

  • If the separator isn’t found, split() returns a list with the original string as its only element.
  • Empty strings can happen if delimiters are consecutive (e.g., "a,,b".split(",") gives ['a', '', 'b']).
  • Don’t forget .rsplit() when you need to split from the right!
Use CaseCode ExampleOutput
Split by space"a b c".split()['a', 'b', 'c']
Split by comma"x,y,z".split(",")['x', 'y', 'z']
Limit splits"a b c".split(" ", 1)['a', 'b c']
Split file extension"file.jpg".split(".")[1]'jpg'
From right side"a/b/c.jpg".rsplit("/", 1)['a/b', 'c.jpg']
def get_base_and_ext(filename):

    base, ext = filename.rsplit(".", 1)

    return base, ext

print(get_base_and_ext("sample_image.png"))

# Outputs ('sample_image', 'png')Code language: PHP (php)

The Python string split method is one of the most powerful tools in your toolbox for working with data, especially when parsing filenames, processing user input, or building tags and URLs for things like Cloudinary.

It’s easy to use, but even more powerful when combined with list indexing, slicing, or loops.

Start Using Cloudinary

Sign up for our free plan and start creating stunning visual experiences in minutes.

Sign Up for Free