Learn Python

Split() in Python

Overview

The Python split() function is a useful tool for breaking down a larger string into smaller strings. It allows developers to split a string based on a specified delimiter and store the resulting substrings as elements in a list.

To use the split() function, provide the delimiter as an argument within the parentheses. The delimiter can be any character or substring that you want to use as a separator. The function searches for occurrences of the delimiter within the string and breaks it down at each occurrence.

Example

# Using whitespace as a delimiter
text = "Hello World! Welcome to Python!"
result = text.split()
print(result)  # Output: ['Hello', 'World!', 'Welcome', 'to', 'Python!']

# Using a comma as a delimiter
fruits = "apple,banana,orange"
result = fruits.split(",")
print(result)  # Output: ['apple', 'banana', 'orange']

Basic Syntax

The basic syntax of the split() function is as follows:

string.split(separator, maxsplit)

  • string: The string to be split.
  • separator: The delimiter at which to split the string. (Optional)
  • maxsplit: The maximum number of splits to perform. (Optional)

If no separator is specified, the function will split the string at every space.

Example

pythonCopy code

sentence = "Hello, how are you today?"
words = sentence.split()
print(words)  # Output: ['Hello,', 'how', 'are', 'you', 'today?']

sentence = "Apple, Banana, Cherry, Date"
fruits = sentence.split(", ")
print(fruits)  # Output: ['Apple', 'Banana', 'Cherry', 'Date']

Parameters of the split() Function

  1. Separator: Character or substring to split the string. Default is whitespace.
  2. Maxsplit: Maximum number of splits. Default is -1, meaning no limit.

Example

text = "apple,banana,orange,grape"
result = text.split(",", 2)
print(result)  # Output: ['apple', 'banana', 'orange,grape']

Handling Edge Cases

Empty Strings

empty_string = ""
result = empty_string.split(",")
print(result)  # Output: ['']

Missing Delimiters

text = "apple banana orange"
result = text.split(",")
print(result)  # Output: ['apple banana orange']

Using Regular Expressions with split()

Regular expressions can be used to define more complex patterns for splitting strings.

Example

import re
text = "apple1banana2cherry3date"
result = re.split(r'\d', text)
print(result)  # Output: ['apple', 'banana', 'cherry', 'date']

Built-in Functions Used with split()

len()

To determine the number of substrings created after splitting:

text = "one two three"
result = text.split()
print(len(result))  # Output: 3

join()

To join a list of substrings back into a single string:

words = ['one', 'two', 'three']
result = " ".join(words)
print(result)  # Output: "one two three"

strip()

To remove leading or trailing whitespace from the substrings:

text = " one , two , three "
result = [x.strip() for x in text.split(",")]
print(result)  # Output: ['one', 'two', 'three']

Conclusion

The split() function is versatile and can be combined with other built-in functions to manipulate and analyze strings effectively.

Written by

Master Python by choosing your ideal learning course

View all courses

Create a free account to access the full topic

Sign up with Google
Sign up with Google
Sign up with JetBrains
Sign up with JetBrains
Sign up with Github
Sign up with GitHub
Coding thrill starts at Hyperskill
I've been using Hyperskill for five days now, and I absolutely love it compared to other platforms. The hands-on approach, where you learn by doing and solving problems, really accelerates the learning process.
Aryan Patil
Reviewed us on