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 a String in Python

Learn how to split strings into substrings, arrays, or lists using Python’s built-in string methods.| …


Updated July 28, 2023

|Learn how to split strings into substrings, arrays, or lists using Python’s built-in string methods.|

How to Split a String in Python

Definition of the Concept

In Python, a string is a sequence of characters enclosed within quotes (single quotes ', double quotes "). When working with strings, it’s often necessary to break them down into smaller parts or substrings. This process is called splitting.

Splitting a string in Python allows you to extract specific words, phrases, or substrings from the original string, making it easier to work with and manipulate the data.

Step-by-Step Explanation

To split a string in Python, follow these steps:

1. Import the String Module (Optional)

While not necessary for basic string manipulation, importing the string module can provide access to useful constants and classes for more advanced tasks.

import string

2. Define Your String

Create a string variable containing the text you want to split.

my_string = "Hello World"

3. Choose the Split Method

Python offers several ways to split a string:

  • split() (default): Splits on whitespace (spaces, tabs, newlines)
  • split(delimiter): Splits on a specific delimiter (e.g., commas, semicolons)

For this example, we’ll use the default split() method.

4. Split the String

Call the split() method on your string variable to split it into substrings.

substrings = my_string.split()

In this case, since we’re using the default split(), Python splits the string on whitespace and returns a list of substrings:

['Hello', 'World']

Example Use Cases

Splitting Strings with Delimiters

Suppose you have a comma-separated string:

comma_string = "apple,banana,cherry"

To split this string into individual fruits using commas as delimiters, pass the delimiter to the split() method:

fruits = comma_string.split(',')
print(fruits)  # Output: ['apple', 'banana', 'cherry']

Splitting Strings with Whitespace

Splitting on whitespace is also useful for extracting words from a string containing multiple words. For instance:

space_string = "This is an example sentence"
words = space_string.split()
print(words)  # Output: ['This', 'is', 'an', 'example', 'sentence']

Conclusion

Splitting strings in Python using the split() method (or specifying a delimiter) allows you to divide text into smaller, more manageable parts. By following these steps and experimenting with different use cases, you’ll become proficient in manipulating strings with ease.

In our comprehensive course on learning Python, we cover string manipulation techniques, including splitting, joining, finding, replacing, and much more!

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

Intuit Mailchimp