Python String split()

The Python String split() method is a built-in function that splits the string based on the specified separator and returns a list of strings.

In this article, we will learn about the Python String split() method with the help of examples.

split() Syntax

The Syntax of split() method is:

str.split(separator, maxsplit)

split() Parameters

The split() method takes two parameters, and both are optional.

  • separator (optional) – Delimiter at which the string split should happen. If not provided, whitespace is taken as a separator, and the string will split at whitespaces. 
  • maxsplit (optional) – An integer that tells us the maximum number of times the split should happen. If not provided, the default value is -1, which means there is no limit on the number of splits.

Example – If you would like to split the string on the occurrences of the first comma, you can set the maxsplit=1

The maxsplit=1 will split the string into 2 chunks—one with the string section before the first comma and another with the string section after the first comma. 

split() Return Value

The split() method returns the list of strings after breaking the given string by the specified character.

Example 1: How split() works in Python?

# splits by whitespace
text = "Python is fun"
print(text.split())

# splits the text after 'is' string
text = "Python is fun"
print(text.split('is'))

# cannot split as the character is not found
text = "Python is fun"
print(text.split('|'))

# splits by comma
fruits ="Apple, Grapes, Orange, Watermelon"
print(fruits.split(','))

Output

['Python', 'is', 'fun']
['Python ', ' fun']
['Python is fun']
['Apple', ' Grapes', ' Orange', ' Watermelon']

Example 2: How split() works when maxsplit is specified?

# splits by whitespace
text = "Python is really fun"
print(text.split(' ', 1))

# splits by comma
fruits = "Apple, Grapes, Orange, Watermelon"
print(fruits.split(',', 0))

# splits by comma
fruits = "Apple, Grapes, Orange, Watermelon"
print(fruits.split(',', 1))

# splits by comma
fruits = "Apple, Grapes, Orange, Watermelon"
print(fruits.split(',', 2))


# splits by comma
fruits = "Apple, Grapes, Orange, Watermelon"
print(fruits.split(',', 3))

Output

['Python', 'is really fun']
['Apple, Grapes, Orange, Watermelon']
['Apple', ' Grapes, Orange, Watermelon']
['Apple', ' Grapes', ' Orange, Watermelon']
['Apple', ' Grapes', ' Orange', ' Watermelon']
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Isupper()

Python String isupper()

Table of Contents Hide isupper() Syntaxisupper() Parameterisupper() Return ValueExample 1: Demonstrating the working of isupper() method Example 2: Practical use case of isupper() in a program Python String isupper() method is…
View Post
Python Callable()

Python callable()

Table of Contents Hide callable() Syntax callable() Parameterscallable() Return ValueExample 1: How does callable() works?Example 2: When an Object is callable Example 3: When an Object is NOT callable  The callable() function in…
View Post
Python All()

Python all()

Table of Contents Hide all() Syntaxall() Parametersall() Return ValueDifference between any() and all() functions in PythonExample 1 – Using all() function on Python ListsExample 2 – Using all() function on…
View Post
Python String Isdigit()

Python String isdigit()

Table of Contents Hide What are the valid digits in Python?isdigit() Syntaxisdigit() Parameters isdigit() Return valueExample 1: Working with Python string isdigit() methodExample 2: How do I check if a string…
View Post