Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Split Strings in Python

Learn how to split strings into substrings using Python’s built-in string methods. Understand the concept, step-by-step examples, and real-world applications. …


Updated July 13, 2023

Learn how to split strings into substrings using Python’s built-in string methods. Understand the concept, step-by-step examples, and real-world applications.

What is String Splitting?

String splitting, also known as tokenization, is a fundamental operation in programming where you break down a string into smaller substrings based on a specific delimiter or criteria. In Python, this is achieved using various built-in functions that make working with strings a breeze.

Why Split Strings in Python?

Splitting strings in Python has numerous applications:

  • Text Processing: Tokenizing text data to extract meaningful information.
  • Data Analysis: Breaking down large datasets into manageable chunks for analysis.
  • Web Development: Handling user input and form data by splitting strings based on specific criteria.

Step-by-Step Guide: Splitting Strings in Python

Here’s a step-by-step example of how to split a string using the split() method:

Example 1: Basic String Split

Suppose you have a string containing comma-separated values, like this:

original_string = "apple,banana,cherry,orange"

To split this string into individual fruits, use the following code:

fruits = original_string.split(",")
print(fruits)  # Output: ["apple", "banana", "cherry", "orange"]

Explanation:

  • The split() method takes a delimiter as an argument. In this case, we’re splitting on commas (,).
  • By default, the split() function splits from left to right and returns a list of substrings.
  • We assign the result to a variable called fruits for easy access.

Example 2: Splitting Strings with Custom Delimiters

Suppose you have a string containing multiple delimiters, like this:

original_string = "apple | banana ! cherry # orange"

To split this string into individual fruits using a custom delimiter (in this case, |, !, and #), use the following code:

fruits = original_string.replace("!", "#").replace("", ";").split("#")
print(fruits)  # Output: ["apple ", " banana ", " cherry", " orange"]

Explanation:

  • First, we replace the ! and | delimiters with # using Python’s built-in string methods (replace()).
  • Then, we split the modified string on the custom delimiter #.

Example 3: Handling Quotes and Escaping Characters

Suppose you have a string containing quotes and escape characters, like this:

original_string = "He said, \"Hello, world!\"."

To split this string into individual words while preserving quotes and escape characters, use the following code:

words = original_string.replace('"', '\\"').split(", ")
print(words)  # Output: ["He", "said,", "Hello,", "world!.", "."]

Explanation:

  • First, we replace double quotes (") with escaped double quotes (\\").
  • Then, we split the modified string on commas and spaces.

Conclusion

Splitting strings in Python is an essential skill that every programmer should master. By understanding how to use built-in functions like split() and custom delimiters, you can efficiently break down text data into smaller substrings. Whether you’re working with text processing, data analysis, or web development, the concepts covered here will help you write cleaner, more efficient code.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp