Python string split() method example

In this source code example, we will demonstrate how to use the Python String split() method to split a string into a list of substrings.

Python string split() method

The split() method splits a string and returns a list of substrings. The following shows the syntax of the split() method:
str.split(sep=None, maxsplit=-1)
The split() method accepts two optional parameters:

Python string split() method example

The following example illustrates how to use the split() method to split a string into multiple words:
s = 'Source Code Examples'
substrings = s.split()
print(substrings)

Output:

['Source', 'Code', 'Examples']
The following example shows how to use the split() method to split a string using the comma (,) separator:

s = 'Source,Code,Examples'
substrings = s.split(',')
print(substrings)

Output:

['Source', 'Code', 'Examples']
The following example illustrates how to use the split() method with the maxsplit parameter:

s = 'Source,Code,Examples'
substrings = s.split(',', 1)
print(substrings)

Output:

['Source', 'Code,Examples']
Since the maxsplit is one, the number of elements in the results list is two.

Related Python String Examples

  1. Python string literals
  2. Python string length example
  3. Python String join() method example
  4. Python string split() method example
  5. Python String index() method example
  6. Python string find() method example
  7. Python string startswith() method example
  8. Python string endswith() method example
  9. Python String lower() method example
  10. Python String upper() method example
  11. Python string title() method example
  12. Python string capitalize() method example
  13. Python string islower() method example
  14. Python string istitle() method example
  15. Python string isupper() method example
  16. Python string swapcase() method example
  17. Python string strip() method example
  18. Python string replace() method example
  19. Python string isdigit() method example
  20. Python string isdecimal() method example
  21. Python string isnumeric() method example
  22. Python string isalpha() method example
  23. Python string isalnum() method example

Comments