In python, a string can be split into smaller chunks using the split() function. split() is a string function that returns a list of words in the string separated by the delimiter string passed. By default, any whitespace is a delimiter.

s = "python is a fun programming language" print(s.split())
Output:
['python', 'is', 'a', 'fun', 'programming', 'language']
Table of Contents
- Syntax
- Examples
Syntax
The following is the syntax to use the split() function:
str.split(sep, maxsplit)
Parameters:
- sep (optional): The separator to use for splitting the string. It’s an optional parameter and if not provided, any whitespace in python is a separator.
- maxsplit: (optional): Specifies the maximum number of splits to be made. Its default value is -1, signifying no limit to the splits.
Returns:
A list of strings resulting from splitting the given string by the separator passed.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Note: There are a number of characters in python that are regarded as whitespace characters. If a separator is not passed, the string is split on the occurrence of any of these whitespace characters. The following is a list of such characters in python:
' '– Space'\t'– Horizontal tab'\n'– Newline'\v'– Vertical tab'\f'– Feed'\r'– Carriage return
You can use the string function isspace() to check if all the characters in a string are whitespace characters or not.
Examples
Example 1: Using the default separator.
s = "python is a fun programming\nlanguage" print(s.split())
Output:
['python', 'is', 'a', 'fun', 'programming', 'language']
In the above example, since a separator is not provided, the default separator (any whitespace character) is used. We can see that the string was split not only at the spaces ' ' but also at the newline character '\n' since it’s also a whitespace character.
Example 2: Using a custom separator.
s = "python-is-a-fun-programming language"
print(s.split("-"))
Output:
['python', 'is', 'a', 'fun', 'programming language']
In the above example, we use dash '-' as our separator. And we can that the string has been split at all the occurrences of '-'. Since there is no '-' between 'programming' and 'language' they have not been split.
Example 3: Specifying a maxsplit
s = "python is a fun programming language" print(s.split(maxsplit=1))
Output:
['python', 'is a fun programming language']
In the above example, we specified the maximum number of splits to be 1. Hence, the string was split only once starting from left.
For more on python’s split() function, refer to the python docs.
Other articles on python strings:
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
