How to Split a String in Python :

How to Split a String in Python
by:
blow post content copied from  Real Python
click here to view original post


Python’s .split() method lets you divide a string into a list of substrings based on a specified delimiter. By default, .split() separates at whitespace, including spaces, tabs, and newlines. You can customize .split() to work with specific delimiters using the sep parameter, and control the amount of splits with maxsplit.

By the end of this tutorial, you’ll understand that:

  • You split a string by spaces in Python using .split() without arguments.
  • Python’s .split() method can split on custom delimiters when you pass a character or string as an argument.
  • You limit splits using maxsplit to control the number of substrings Python extracts.
  • .splitlines() splits multiline strings into individual lines, excluding or including line breaks with the keepends parameter.
  • re.split() uses regular expressions for splitting strings based on complex patterns.

Exploring these methods will level up your text-processing capabilities and enable you to confidently tackle real-world data parsing challenges.

Take the Quiz: Test your knowledge with our interactive “How to Split a String in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

How to Split a String in Python

In this quiz, you'll test your understanding of Python's .split() method. This method is useful for text-processing and data parsing tasks, allowing you to divide a string into a list of substrings based on a specified delimiter.

How to Split a String in Python Using .split()

The .split() method in Python is a versatile tool that allows you to divide a string into a list of substrings based on a specified delimiter. By default, .split() separates a string at each occurrence of whitespace, which includes spaces, tabs, and newlines. This makes it particularly handy when dealing with plain text where words are separated by spaces:

Python
>>> sentence = "Python is a versatile language."
>>> sentence.split()
['Python', 'is', 'a', 'versatile', 'language.']

As you can see, .split() divides the sentence into individual words, removing the spaces in the process. It’s important to note that .split() also strips newlines by default, which might not be immediately obvious:

Python
>>> text = """Line one
... Line two
... Line three"""

>>> text.split()
['Line', 'one', 'Line', 'two', 'Line', 'three']

The .split() method treats the newline characters as whitespace, and splits the text into individual words rather than lines. This is a crucial point to remember when working with multiline strings.

The simplicity of .split() makes it a powerful tool for string manipulation. Whether you’re processing text files or parsing user input, using .split() can streamline your workflow.

When you’re faced with the task of cleaning up messy text, you may want to pair .split() with .strip(). You can learn more about using str.strip().split() by expanding the collapsible section below:

You may encounter code that uses .split() in combination with another string method, .strip():

Python
>>> text = "    Hello   world  "
>>> text.strip().split()
['Hello', 'world']

In this example, you’re dealing with text that contains whitespace characters at the beginning and end of text. You may see results like this when working with data that you received through web scraping, copy-paste artifacts from PDFs, or even regular user input.

Combining .strip() and .split() is a common and straightforward way to break potentially messy text into manageable tokens.

The idea is that you first remove superfluous whitespace characters from the beginning and end of the string using .strip(). This operation returns a new string object without that whitespace. You then chain .split() to this cleaned output, allowing it to break the text into elements—in this case, individual words.

If you’re only dealing with messy whitespace, however, then you don’t need to use .strip() to get to a clean result:

Python
>>> text = "    Hello   world  "
>>> text.split()
['Hello', 'world']

Because .split() splits on any whitespace and ignores extra leading and trailing whitespace, the result is the same as when you first explicitly remove leading and trailing whitespace with .strip().

Nevertheless, there may be advantages to adding .strip() into the mix:

  • Explicitness: You clearly communicate to other developers that you intend to apply data cleaning—and readability counts!
  • Future-proofing: You may later need to update your code to split on a different delimiter, in which case .split() won’t remove extra whitespace automatically. A well-designed additional step of explicitly removing whitespace with .strip() can prevent surprises.

If you’re only splitting on whitespace, then you likely won’t need to use str.strip().split(). However, due to developer habits and a preference for explicitness, you may still come across this combination of string methods in the wild.

When you use .split(), it returns a list of substrings. This means that you can iterate over the result, access individual elements using indexing, or unpack the iterable into separate variables.

For example, when you’re dealing with user input, you might want to extract specific pieces of information:

Python
>>> user_input = "Deborah Lopez 30"
>>> user_data = user_input.split()
>>> user_data
['Deborah', 'Lopez', '30']

>>> name, surname, age = user_data
>>> print(f"Welcome {name}! You're {age} years old.")
Welcome Deborah! You're 30 years old.

In this code snippet, you take a string containing a name, surname, and age, and split it into a list of three separate strings. Then, you unpack the list into three descriptive variables. Finally, you use an f-string to format the output.

In this section, you’ve learned how to use Python’s .split() method to divide strings into smaller parts based on whitespace. This method is invaluable when working with plain text data, allowing you to extract and manipulate information. By understanding the default behavior of .split(), including its treatment of newlines, you’ll be well-equipped to handle a variety of string manipulation tasks in your Python projects.

In the next section, you’ll explore how to customize the behavior of .split() by specifying different delimiters, enabling you to tackle more complex string splitting scenarios.

Split With Different Delimiters Using sep

By default, .split() uses any whitespace as a delimiter to separate a string into a list of substrings. However, many real-world scenarios require splitting strings using other delimiters. This is where the sep parameter comes into play.

Imagine that you’re parsing data from a CSV (comma-separated values) file. CSV files are a popular format for storing tabular data, where each line represents a row and each value within a line is separated by a comma.

Read the full article at https://realpython.com/python-split-string/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


February 05, 2025 at 07:30PM
Click here for more details...

=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce