top of page
Writer's pictureSofia Sondh

How to Split a String in Python?

Updated: Feb 10, 2023



Split string in Python refers to the process of breaking a larger string into smaller substrings or pieces based on a specified delimiter or pattern. In this article, you will learn about How to Split a String in Python with different methods.


The split function in Python is used to perform this task and it takes the string to be split and the delimiter as arguments. The function returns a list of substrings that are obtained by splitting the input string based on the specified delimiter.


Methods to Split String in Python

In Python, there are several ways to split a string into substrings:

  1. split() method: This is the most common method for splitting a string into substrings. It splits the string into a list of substrings using a specified delimiter string, or whitespace characters if the delimiter is not specified.

  2. rsplit() method: This is similar to the split() method, but it splits the string from the right side to the left.

  3. splitlines() method: This method splits the string into a list of strings based on line breaks (\n or \r).

  4. Regular expressions (regex): You can use the re-module and regular expressions to split a string into substrings. This approach is more flexible and powerful than the split() method, but it can be more complex to use.

  5. partition() method: This method splits the string into three parts: the part before the specified delimiter, the delimiter itself, and the part after the delimiter.

  6. shlex.split() function: This function splits a string into substrings, taking into account quotes and escaped characters. It is useful for splitting shell-style command lines.

  7. String slicing: You can split a string into substrings by slicing the string into parts. This approach is simple but less flexible and less readable than other methods.


1. Split() Method

The split() method in Python is used to split a string into a list of substrings, using a specified delimiter string. If the delimiter is not specified, it defaults to any white space characters (spaces, tabs, and newlines).


Here's the syntax for the split() method:

string.split(separator, maxsplit)

where:

  • string is the string that you want to split.

  • separator (optional) is the delimiter string that is used to split the string. If the separator is not specified, any white space characters in the string will be used as the delimiter.

  • maxsplit (optional) is the maximum number of splits to make. If maxsplit is specified, the split() method will make at most maxsplit + 1 splits.

Here are some examples to demonstrate the usage of the split() method:


Example 1:

string = "Hello world!"
result = string.split()
print("Actual String is -", string)
print("Splitted String is -",result)

Output:


Example 2:

string = "Hello, world, how are you?"
result = string.split(", ", 2)
print("Actual String is - ", string)
print("Splitted String is -",result)

Output:


Note: The split() method returns a list of substrings, which you can access and manipulate just like any other list in Python.


2. rsplit() Method

The rsplit() method in Python is used to split a string into a list of substrings, starting from the right side of the string and working toward the left. The rsplit() method is similar to the split() method, but it splits the string from the right side to the left.


Here's the syntax for the rsplit() method:

string.rsplit(separator, maxsplit)

where:

  • string is the string that you want to split.

  • separator (optional) is the delimiter string that is used to split the string. If the separator is not specified, any white space characters in the string will be used as the delimiter.

  • maxsplit (optional) is the maximum number of splits to make. If maxsplit is specified, the rsplit() method will make at most maxsplit + 1 splits.

Here's an example demonstrating the usage of the rsplit() method:

string = "Hello, world, how are you?"
result = string.rsplit(", ", 2)
print("Actual String is - ", string)
print("Splitted String is -",result)

Output:


Note: The rsplit() method returns a list of substrings, which you can access and manipulate just like any other list in Python.


3. splitlines() Method

The splitlines() method in Python is used to split a string into a list of strings based on line breaks (\n or \r). The splitlines() method is useful for splitting multi-line strings into a list of strings, where each element in the list corresponds to a line in the original string.

Here's the syntax for the splitlines() method:

string.splitlines(keepends)

where:

  • string is the string that you want to split.

  • keepends (optional) is a Boolean value that determines whether line break characters should be included in the returned strings. If keepends is True, the line break characters will be included. If keepends is False (the default), the line break characters will be omitted.

Here's an example demonstrating the usage of the splitlines() method:


Example 1:

string = "Hello,\nworld,\nhow are you?"
result = string.splitlines()
print("The String is -", string)
print("Splitted String is -", result)

Output:


Example 2:

string = "Hello,\nworld,\nhow are you?"
result = string.splitlines(True)
print("The String is -", string)
print("Splitted String is -", result)

Output:


Note: The splitlines() method returns a list of strings, which you can access and manipulate just like any other list in Python.


4. Regular Expression Method

In Python, you can use regular expressions (regex) to split a string into substrings. The re-module provides the re.split() function for this purpose. Regular expressions are a powerful and flexible tool for pattern matching, and they can be used to split a string into substrings in complex and sophisticated ways.


Here's an example demonstrating the usage of the re.split() function:

import re
string = "Hello, world, how are you?"
result = re.split(", | ", string)
print(result)

# Output: ['Hello', 'world', 'how', 'are', 'you?']

In this example, the re.split() function splits the string 'string' into a list of substrings using the regular expression ", | " as the delimiter. The regular expression ", | " matches either a comma followed by a space, or a space character, so it splits the string into substrings on either type of delimiter.


Note: Regular expressions can be complex and difficult to understand, so it's a good idea to study the basics of regex and try out a few examples before using them for splitting strings in your own code.


5. partition() Method

The partition() method in Python is used to split a string into three parts: a prefix, a separator, and a suffix. The partition() method splits the string at the first occurrence of the separator and returns a tuple containing the prefix, separator, and suffix.


Here's the syntax for the partition() method:

string.partition(separator)

where:

  • string is the string that you want to split.

  • separator is the delimiter string that is used to split the string.

Here's an example demonstrating the usage of the partition() method:

string = "Hello, world, how are you?"
result = string.partition(", ")
print(result)

# Output: ('Hello', ', ', 'world, how are you?')

In this example, the partition() method splits the string 'string' into three parts using the ", " separator. The first part of the returned tuple is the prefix ('Hello'), the second part is the separator (', '), and the third part is the suffix ('world, how are you?').


Note: The partition() method only splits the string once, at the first occurrence of the separator. If you want to split the string into substrings on multiple occurrences of the separator, you can use the split() or rsplit() method instead


6. Shlex.split() function

The shlex.split() method in Python is used to split a string into a list of tokens, treating the string as a shell command line. The shlex.split() method is useful for splitting shell commands into a list of arguments, preserving quotes, and escaping characters.


Here's an example demonstrating the usage of the shlex.split() method:

import shlex
string = "ls -l --color=always -a /etc"
result = shlex.split(string)
print(result)

# Output: ['ls', '-l', '--color=always', '-a', '/etc']

In this example, the shlex.split() method splits the string 'string' into a list of tokens, treating the string as a shell command line. The returned list result contains the individual arguments of the command line, including flags (-l, -a) and options (--color=always).


Note: The shlex.split() method is specifically designed for splitting shell commands into a list of arguments, so it is not suitable for general string splitting tasks. For more general string splitting, you should use the split() or rsplit() method instead.


7. String slicing Method

In Python, you can also split a string into substrings using string slicing. String slicing is a way of extracting substrings from a string by specifying the starting and ending indices of the substring.


Here's an example demonstrating the usage of string slicing to split a string:

string = "Hello, world, how are you?"
result = [string[i:j] for i, j in zip(range(0, len(string), 13), range(13, len(string)+13, 13))]
print(result)

# Output: ['Hello, world', ' how are you?']

In this example, the code uses a list comprehension to split the string 'string' into substrings of length 13. The starting and ending indices of each substring are generated using the range() and zip() functions, and the substrings are extracted from the string using slicing.


Note: String slicing is a low-level method for splitting strings, and it is generally less flexible and more difficult to use than the other methods discussed (such as split(), rsplit(), and partition()). You should consider using these other methods unless you have a specific need for string slicing.


Which is the best method to split a string in Python?

The best method to split a string in Python depends on the specific requirements of the task at hand.

  • If you want to split a string into substrings based on a delimiter, split() and rsplit() are the simplest and most flexible methods to use.

  • If you want to split a string into three parts: a prefix, a separator, and a suffix, then partition() is the best method to use.

  • If you want to split a shell command into a list of arguments, preserving quotes and escaping characters, then shlex.split() is the best method to use.


Conclusion:

If you need to split a string into substrings based on a delimiter, split() and rsplit() are the best methods to use. They are simple, flexible, and widely used, making them the most convenient and accessible methods for most string splitting tasks in Python.

0 comments

Recent Posts

See All

Comments


bottom of page