Skip to main content

How to split a string into a list of substrings in Python

How to split a string into a list of substrings in Python.

Here's a step-by-step tutorial on how to split a string into a list of substrings in Python.

  1. Start by defining a string that you want to split. For example:
string = "Hello, world! Welcome to Python."
  1. Decide on a delimiter that you want to use to split the string. The delimiter is a character or a sequence of characters that will be used as a boundary for splitting. For instance, if you want to split the string at each space, you can use a space as the delimiter.

  2. Use the split() method to split the string into a list. This method is available for any string object in Python. It takes an optional argument, the delimiter, and returns a list of substrings.

   substrings = string.split()

This will split the string at each space and return the following list:

   ['Hello,', 'world!', 'Welcome', 'to', 'Python.']

If you want to split the string at a specific character other than a space, you can pass that character as the delimiter. For example, to split the string at each comma, you can use:

   substrings = string.split(',')

This will return the following list:

   ['Hello', ' world! Welcome to Python.']
  1. If you want to split the string into a fixed number of substrings, you can pass an additional argument to the split() method. This argument specifies the maximum number of splits to be performed.
   substrings = string.split(' ', 2)

This will split the string at the first two spaces and return the following list:

   ['Hello,', 'world!', 'Welcome to Python.']

Note that the maximum number of splits is not guaranteed. If there are fewer occurrences of the delimiter than the specified maximum, the resulting list will have fewer elements.

  1. If you want to split the string based on a more complex pattern, you can use regular expressions (regex) with the re module. This allows you to split the string using a pattern instead of a specific character.
   import re
substrings = re.split(r'\W+', string)

This will split the string at any non-alphanumeric character and return the following list:

   ['Hello', 'world', 'Welcome', 'to', 'Python']

In this example, the regex pattern \W+ matches one or more non-alphanumeric characters.

That's it! You now know how to split a string into a list of substrings in Python.