How to Use Split in Python to Split a String

python logo

Python is the world’s second most popular programming language and is the preferred choice of many programmers. It is particularly useful for backend web development, artificial intelligence, data analysis, and scientific computing, but can also be used for creating games and desktop apps.

One of the most common questions when it comes to Python is how to split a string. A string in Python is a sequence of Unicode characters, and string variables can be very helpful for Python programmers. They can contain alphanumeric or numeric information, and are often used to print messages or store data directories.

The .split() Python function is one of the most commonly used string manipulation tools. It is the opposite of the concatenation function which joins two strings together. split() scans through a string of characters and separates it whenever it comes across a pre-specified separator. It uses two parameters: separator and maxsplit.

  • The separator parameter defines where to break the string. This is not, however, a compulsory parameter, as if left undefined, Python will automatically use any whitespace as a separation point.
  • The maxsplit parameter defines the maximum number of times that the string can be broken up. Again this is not mandatory and if no value is added here, Python will scan the whole string and separate it whenever it finds a delimiter.

Examples of using split() in Python

One of the simplest examples as to how to divide Python strings using split() is to assign no parameters and ask it to break down a string of text into individual words:

Example

text = 'The best OS in the world is Linux'
# Split the text wherever there's a space.
words = text.split()
print(words)

The code above will give the following output:

['The', 'best ', 'OS', 'in', 'the', 'world', 'is', 'Linux']

Extract multiple pieces of information

The Python split() function can also be used to extract multiple pieces of information from a single string and then assign each to a separate variable. The following example is of a paragraph where each sentence is turned into a variable:

paragraph = 'The speedy orange fox jumps over the lazy cat. The speedy orange cat jumps over the lazy fox'
# Split the text wherever there's a full stop.
a,b = paragraph.split('.')
# Display the results.
print(a)
print(b)

If you only want to break up the first part of a string, remember to use the maxsplit parameter. See the example below:

animals= 'dog and cat and rabbit and fish and otter'
# maxsplit of 1
print(animals.split(' and ',1))
# maxsplit of 2
print(animals.split(' and ',2))
# maxsplit of 1
print(animals.split(' and ',3))
# maxsplit of 1
print(animals.split(' and ',4))

The resulting output of the code snippet above will be:

['dog ', 'cat and rabbit  and fish and otter']
['dog ', 'cat ', 'rabbit and fish and otter']
['dog ', 'cat ', 'rabbit', 'fish and otter']
['dog ', 'cat ', 'rabbit', 'fish', 'otter']

If you do specify maxsplit and there are a sufficient number of delimiting pieces of text in the string, the output will have a length of maxsplit+1.

Python split() only works on string variables. If you are trying to use it on a non-string object, you can actually force Python to treat the variable as a string by using str(x).

How to create a string in Python?

Strings can be made by enclosing characters inside a single quote or double-quotes. See below example:

# defining strings in Python
# all of the following are equivalent
my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)

When you run the program, the output will be:

Hello
Hello

Common Python String Methods

There are numerous methods available with the string object. The format() method is one of them. Some of the other regularly used methods are lower(), upper(), join(), split(), find(), and replace().

SHARE:

Leave a Reply

Your email address will not be published. Required fields are marked *