Python Split() Function: Syntax, Parameters, Examples
By Rohit Sharma
Updated on Sep 22, 2025 | 25 min read | 60.47K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Sep 22, 2025 | 25 min read | 60.47K+ views
Share:
Table of Contents
Have you ever had a long string of text, like a sentence or a line from a data file, that you needed to break into smaller, manageable parts? This is a common task in programming, and the split function in python is the perfect tool for the job. It is one of the most fundamental and frequently used string methods, acting as the primary way to parse and organize string data.
In this blog, you will learn everything there is to know about splitting strings. We will cover the basic syntax, explore its powerful parameters with detailed examples, discuss common use cases, and look at related string methods. By the end, you will be able to confidently use this function to handle any string parsing challenge.
Enroll for the Artificial Intelligence & Machine Learning Courses from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.
Popular Data Science Programs
The split function in python breaks a larger string into a list of smaller strings. The original string remains unchanged, and the function returns a new list containing the substrings. Think of it like taking a sentence and using the spaces as scissors. You cut the sentence at each space, and you are left with a collection of individual words. This collection is what Python calls a list.
This function is the go-to method for parsing data. If you have data separated by commas, tabs, or any other character, the split() method is how you separate those values for individual processing.
Using the Python split() function is more than just calling a method, it’s about knowing when and how to apply it to handle text efficiently. Here are three programs that can help you:
Basic Syntax
The basic syntax for the method is:
Python
string.split(separator, maxsplit)
The function has two optional parameters:
We will explore both of these parameters in detail in the next section.
Understanding how to use the split function in python comes down to understanding its parameters. Let's start with the simplest case and build up to more complex examples.
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
When you call the split() method without providing any arguments, it performs a special kind of split. It splits the string by any sequence of whitespace. This includes spaces, tabs (\t), and newlines (\n). A key feature of this default behavior is that it treats multiple whitespace characters as a single delimiter and discards any empty strings from the result.
Example 1: Splitting by Spaces
Python
sentence = "Python is a powerful programming language"
words = sentence.split()
print(words)
Output:
['Python', 'is', 'a', 'powerful', 'programming', 'language']
Example 2: Handling Multiple Whitespace Characters
Notice how the multiple spaces between "is" and "fun" and the tab character are all handled gracefully.
Python
phrase = "Learning Python is\tfun"
parts = phrase.split()
print(parts)
Output:
['Learning', 'Python', 'is', 'fun']
This default behavior is very useful for processing natural language text where spacing can be inconsistent.
The real power of the split function in python comes from the separator parameter. You can specify any character or string to act as the delimiter. When you provide a specific separator, the behavior changes slightly: consecutive separators will result in empty strings in your output list.
Example 1: Splitting a Comma-Separated String
This is one of the most common use cases, especially when working with data from CSV files.
Python
csv_data = "apple,banana,cherry,orange"
fruits = csv_data.split(',')
print(fruits)
Output:
['apple', 'banana', 'cherry', 'orange']
Example 2: Splitting by a Different Character
You can use any character as the delimiter.
Python
date = "2025-09-22"
date_parts = date.split('-')
print(date_parts)
Output:
['2025', '09', '22']
Example 3: Handling Consecutive Separators
When a specific separator is used, split() will create an empty string for every pair of consecutive delimiters.
Python
record = "John;;Doe;New York"
fields = record.split(';')
print(fields)
Output:
['John', '', 'Doe', 'New York']
This output includes an empty string '' because there was nothing between the two semicolons.
Also Read: 16+ Essential Python String Methods You Should Know (With Examples)
The second optional parameter, maxsplit, controls the maximum number of splits that will occur. The resulting list will have at most maxsplit + 1 elements. This is useful when you only need to separate the first few parts of a string and leave the rest intact.
Example 1: Using maxsplit=1
This will split the string only at the first occurrence of the separator.
Python
log_entry = "INFO:User logged in successfully"
parts = log_entry.split(':', maxsplit=1)
print(parts)
Output:
['INFO', 'User logged in successfully']
This is a great way to separate a key from its value.
Example 2: Using maxsplit=2
Let's see what happens when we increase the number of splits.
Python
url_path = "/user/profile/settings/security"
path_components = url_path.split('/', maxsplit=2)
print(path_components)
Output:
['', 'user', 'profile/settings/security']
The string was split at the first and second slashes, and the rest of the string was left as the final element in the list. Note the first empty string is due to the leading /.
Also Read: Top 43 Pattern Programs in Python to Master Loops and Recursion
Now that we understand what does split function do in python and its parameters, let's look at some real-world scenarios where it is incredibly useful.
Imagine you have a text file (data.csv) with the following content:
name,age,city
Alice,30,New York
Bob,25,Los Angeles
You can read this file line by line and use split(',') to process each record.
Python
with open('data.csv', 'r') as file:
for line in file:
# strip() removes leading/trailing whitespace, including the newline character
fields = line.strip().split(',')
print(f"Fields: {fields}")
Output:
Fields: ['name', 'age', 'city']
Fields: ['Alice', '30', 'New York']
Fields: ['Bob', '25', 'Los Angeles']
You can ask a user to enter multiple values on a single line and use the split function in python to separate them.
Python
user_input = input("Enter your first name and last name, separated by a space: ")
names = user_input.split()
first_name = names[0]
last_name = names[1]
print(f"Hello, {first_name} {last_name}")
Counting Words in a Sentence
A simple and classic task made easy by split().
Python
sentence = "This is a sample sentence with several words."
words = sentence.split()
word_count = len(words)
print(f"The sentence has {word_count} words.")
Output:
The sentence has 8 words.
Log files often have a structured format. You can use split() to parse these lines. Using maxsplit is particularly helpful here.
Python
log_line = "ERROR:2025-09-22:Database connection failed: timeout expired"
parts = log_line.split(':', maxsplit=2)
log_level = parts[0]
timestamp = parts[1]
message = parts[2]
print(f"Level: {log_level}")
print(f"Timestamp: {timestamp}")
print(f"Message: {message}")
Output:
Level: ERROR
Timestamp: 2025-09-22
Message: Database connection failed: timeout expired
Python's string toolkit is extensive. Understanding methods related to split() can help you write cleaner code.
The rsplit() method behaves exactly like split() with one key exception: when the maxsplit parameter is used. As its name suggests, rsplit() starts splitting from the right side of the string.
Let's compare them directly:
Python
data_string = "item1-item2-item3-item4"
# Using split() with maxsplit
print("split():", data_string.split('-', maxsplit=1))
# Using rsplit() with maxsplit
print("rsplit():", data_string.rsplit('-', maxsplit=1))
Output:
split(): ['item1', 'item2-item3-item4']
rsplit(): ['item1-item2-item3', 'item4']
As you can see, split() cut at the first hyphen from the left, while rsplit() cut at the first hyphen from the right.
Also Read: Exception Handling in Python
If your only goal is to split a string by line breaks, splitlines() is the specialized tool for the job. It correctly handles different types of line endings (like \n and \r\n).
Python
multi_line_string = "First line\nSecond line\r\nThird line"
lines = multi_line_string.splitlines()
print(lines)
Output:
['First line', 'Second line', 'Third line']
If you only need to split a string into three parts based on the first or last occurrence of a separator, partition() is a great choice. It always returns a tuple of three elements:
Python
email = "contact@example.com"
parts = email.partition('@')
print(parts)
Output:
('contact', '@', 'example.com')
Also Read: String Methods Python
While the split function in python is straightforward, a few common issues can trip up beginners.
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
A common pattern is to split a string of numbers and then convert them to integers. If the string contains a non-numeric value, this will raise a ValueError.
Python
numbers_str = "1 2 3 four 5"
numbers = numbers_str.split()
for num_str in numbers:
try:
number = int(num_str)
print(number)
except ValueError:
print(f"Could not convert '{num_str}' to an integer.")
Using a try-except block makes your code more resilient.
Remember that when you use a specific separator, you can get empty strings in your result. This happens if your string starts or ends with the separator, or if you have multiple separators in a row.
Python
path = "/home/user/data/"
parts = path.split('/')
print(parts)
Output:
['', 'home', 'user', 'data', '']
You can filter these out easily using a list comprehension if they are not needed:
Python
filtered_parts = [part for part in parts if part]
print(filtered_parts)
Output:
['home', 'user', 'data']
The split function in python is a fundamental building block for almost any data processing task. It is simple enough for beginners to grasp quickly but has the flexibility with its separator and maxsplit parameters to handle complex parsing jobs. Mastering this function and its relatives like rsplit() and splitlines() will make your string manipulation code cleaner and more powerful. Now that you have a deep understanding of this tool, try applying it to your own data parsing challenges.
Master essential concepts with our trending Data Science Courses. Scroll through the programs below to find the right one for you.
Boost your career with our Data science skill pages. Explore the various skills to take your expertise to the next level.
SL. No | Top Data Science Skills to Learn | |
1 |
Data Analysis Online Courses | Inferential Statistics Online Courses |
2 |
Hypothesis Testing Online Courses | Logistic Regression Online Courses |
3 |
Linear Regression Courses | Linear Algebra for Analysis Online Courses |
The split() function always returns a list of strings. This is because the purpose of the function is to create a collection of the substrings that result from the split. Even if the separator is not found in the string, the function will still return a list containing a single element: the original, unmodified string.
Yes, the separator used in the split() function is case-sensitive. This means that if you try to split the string 'Apple-Ball-Cat' using the separator 'b', it will not find a match and will not split the string. For a case-insensitive split, you would first need to convert the entire string to a consistent case, for example, by using the .lower() method, and then split the resulting lowercase string.
The built-in str.split() method does not support regular expressions; it only works with literal string separators. However, Python's dedicated regular expression module, re, provides a re.split() function for this exact purpose. This is extremely powerful for splitting a string based on more complex patterns or multiple different delimiters at once.
The most direct and Pythonic way to split a string into its characters is to use the list() constructor. Simply passing the string to list() will produce a list of its characters, for example, list('python') returns ['p', 'y', 't', 'h', 'o', 'n']. You should not use the split() method for this, as attempting to split by an empty string (split('')) will raise a ValueError.
Attempting to call split('') on a string will result in a ValueError. Python does this to prevent ambiguous behavior. Splitting a string by "nothing" could logically result in a list of individual characters separated by empty strings or even an infinite number of empty strings. To avoid this confusion, Python explicitly disallows it.
No, the split() function does not modify the original string. In Python, strings are immutable, which means they cannot be changed after they are created. When you call split(), the original string is left untouched, and the function creates and returns a completely new list object that contains the resulting substrings.
When you call split() with no arguments, it activates a special splitting algorithm. It splits the string by any sequence of whitespace characters, including spaces, tabs (\t), newlines (\n), and returns (\r). Importantly, it treats multiple whitespace characters in a row as a single delimiter and will not produce empty strings in the output, which is very convenient for cleaning up text.
This is a common point of confusion. The default split() splits on any sequence of whitespace and discards empty strings. In contrast, split(' ') splits only on the single space character. This means that if you have multiple spaces in a row, split(' ') will produce empty strings in your list. For example, 'hello world'.split() returns ['hello', 'world'], while 'hello world'.split(' ') returns ['hello', '', 'world'].
To combine a list of strings into one, you use the str.join() method. The syntax might seem backward at first: you call the method on the separator string you want to use. For example, to join the list my_list = ['a', 'b', 'c'] with a hyphen, you would write '-'.join(my_list), which would produce the string 'a-b-c'.
Yes, the separator can be a string of any length. This allows you to split a string based on a multi-character delimiter. For instance, you could split a piece of text into paragraphs by using a double newline character as a separator, like text.split('\n\n'), or split a string by a specific word like text.split('CHAPTER').
Python does not impose a predefined limit on the number of elements the list can contain. The practical limit is determined by your computer's available memory (RAM). If you split a very large string into an extremely large number of substrings, your program could consume all available memory and crash with a MemoryError.
The behavior depends on the arguments. If you call split() on an empty string with a separator (e.g., ''.split(',')), it returns a list containing a single empty string: ['']. However, if you call it with no arguments (''.split()), it returns an empty list [], because its default behavior is to find sequences of whitespace, and there are none in an empty string.
Yes, you can provide a negative number for the maxsplit parameter. A negative value, such as -1, is interpreted by Python as having no limit on the number of splits. Its behavior is identical to omitting the maxsplit parameter entirely, meaning the string will be split on every occurrence of the separator.
The rsplit() method is most commonly used when you need to split off the last part of a string. A classic example is separating a filename from its extension, especially when the filename itself might contain dots. Using filename.rsplit('.', maxsplit=1) ensures you split only on the final dot, correctly handling files like 'project.v1.2.zip'.
The standard str.split() method can only handle a single, consistent separator. When you need to split a string by several different characters (for example, by commas, semicolons, and pipes), you should use the re.split() function from Python's regular expression module. This function allows you to define a pattern that includes all of your desired delimiters.
Yes, the split() method consumes the separator during the splitting process, so the separator itself does not appear in any of the substrings in the final list. If you need to keep the delimiter in your result, you should use the partition() method, which returns a three-part tuple containing the part before the separator, the separator itself, and the part after it.
The time complexity of split() is generally O(n), where n is the length of the string. This is because, in the worst-case scenario, the function must iterate through every character of the string to find all the occurrences of the separator. The performance is therefore considered linear, as the execution time grows in direct proportion to the size of the input string.
The separator argument for the split() function must be a string. You cannot pass an integer or float directly. If you want to split a string by a digit, you must provide that digit as a string character. For example, to split 'chapter1section2' by the digit 1, you would use 'chapter1section2'.split('1').
By default, splitlines() does not create an extra empty string if the main string ends with a newline. However, the method accepts an optional boolean argument called keepends. If you call splitlines(True), the line break characters (\n, \r\n) will be kept at the end of each string in the resulting list, which can be useful for certain text processing tasks.
Yes, the concept of a split function is fundamental to string manipulation and is found in nearly all modern programming languages. For instance, JavaScript has a .split(), Java has a .split(), and C# has a .Split() method. While the core functionality is the same, the specific behaviors, such as default parameters and regex handling, can vary between languages.
834 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources