View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Python Split() Function: Syntax, Parameters, Examples

By Rohit Sharma

Updated on May 30, 2025 | 25 min read | 59.4K+ views

Share:

Did you know?

In early 2025, Python’s split() function became a secret weapon in AI research!

Developers discovered that cleverly tweaking the maxsplit parameter can speed up natural language processing tasks by up to 30%, helping chatbots understand sentences faster and more accurately!

The Python split() function is a powerful tool for breaking down strings into smaller parts. Often, when dealing with large blocks of text, it can be difficult to work with. Using split() allows you to separate words or phrases based on a specified delimiter easily. 

In this article, you’ll learn how to use the Python split() function, its parameters, and how it can simplify text manipulation, making your coding tasks much more efficient.

Improve your coding skills with upGrad’s online software engineering courses. Specialize in cybersecurity, full-stack development, and much more. Take the next step in your learning journey!

What is Python Split() Function?

The Python split() function is a built-in method used to divide a string into a list of substrings based on a specified delimiter. By default, it separates the string wherever there is whitespace, but you can also define custom separators such as commas, spaces, or special characters. 

The Python split() function has two main parameters:

1. Separator: 

This is the character or string where the split occurs. It defines the boundary between substrings. If no separator is provided, Python uses whitespace (spaces, tabs, or newlines) as the default separator. 

You can specify any character or string, such as a comma (,), colon (:), or even a custom pattern, making the function versatile for different use cases.

2. Maxsplit: 

This parameter limits the number of splits Python will perform. By default, its value is -1, meaning there’s no limit to the number of splits. 

When you specify a number for maxsplit, the function splits the string only that many times, and the remainder of the string will be returned as the last item in the list. This is useful when you only need a specific number of divisions from the original string.

Together, these parameters give you control over how strings are divided, making the Python split() function a flexible and powerful tool for text manipulation.

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 prgrams that can help you:

The syntax of the Split function is as follows:

string.split(separator,max) 

The Python string Split function returns a list of words after separating the string or line with the help of a delimiter string such as the comma ( , ) character. 

Some of the merits of using Split function in Python are listed as follows: 

  • It is useful in situations where you need to break down a large string into smaller strings. 
  • If the separator is not present within the split function, the white spaces are considered as separators. 
  • The split function helps to analyze and deduce conclusions easily. 
  • It is also useful in decoding strings encrypted in some manner. 

Also Read16+ Essential Python String Methods You Should Know (With Examples)

background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

String variables in Python contain numeric and alphanumeric data which are used to store data directories or display different messages. They are very useful tools for programmers working in Python.  

The .split() method is a beneficial tool for manipulating strings. It returns a list of strings after the main string is separated by a delimiter. The method returns one or more new strings and the substrings also get returned in the list datatype.  

A simple example of the split function is as follows:

x = ‘red,orange,yellow’ 
x.split(“,”) 

Output:

['red', 'orange', 'yellow']

Here, the string x contains three color names separated by commas. When you use the Python split() function with a comma as the separator, it breaks the string at every comma and returns a list of the parts between them. This way, you get each color as a separate element in the list.

With this basic understanding, let’s dive into the different ways you can use the split() function to handle a variety of string-splitting needs.

If you're still building your Python skills, now is the perfect time to strengthen that foundation. Check out the Programming with Python: Introduction for Beginners free course by upGrad to build the foundation you need before getting into programming.

What are the Different Ways of Using the Split Function? 

When working with strings, one size doesn’t fit all. You might need to split text by spaces, commas, newlines, or even multiple delimiters, depending on the data you’re handling. That’s where knowing the different ways of using the Python split() function becomes essential. Without this flexibility, you could end up writing clunky, repetitive code trying to handle every case manually. 

Before we explore these techniques, make sure you’re comfortable with basic Python strings and the core idea behind the Python split() function.

A Python split() function can be used in different ways. They are:

  • Splitting String by Space
  • Splitting String on first occurrence
  • Splitting a file into a list
  • Splitting a String by newline character (\n)
  • Splitting a String by tab (\t)
  • Splitting a String by comma (,)
  • Splitting a String with multiple delimiters
  • Splitting a String into a list
  • Splitting a String by hash (#)
  • Splitting a String using maxsplit parameter
  • Splitting a String into an array of characters
  • Splitting a String using substring

The different techniques are explained below: 

1. Splitting String by Space 

The split() method in Python splits the string on whitespace if no argument is specified in the function. An example of splitting a string without an argument is shown below: 

str = “Python is cool” 
print(str.split()) 

The output of the above code is as follows:

[‘Python’, ‘is’, ‘cool’] 

In the example above, we have declared variable str with a string value. You can see that we have not defined any arguments in the Split function, so the string gets split with whitespaces.  

2. Splitting String on first occurrence 

When we split a string based on the first occurrence of a character, it results in two substrings – the first substring contains the characters before the separator and the second substring contains the character after the separator.  

An example of splitting a string on the first occurrence of a character is shown below: 

str = “abcabc” 
print(str.split(c)) 

The output of the above code is as follows: 

[‘ab’, ‘abc’] 

Here, we have declared str with a string value “abcabc”. The split function is implemented with separator as “c” and maxsplit value is taken as 1. Whenever the program encounters “c” in the string, it separates the string into two substrings  – the first string contains characters before “c” and the second one contains characters after “c”.  

3. Splitting a file into a list 

When you want to split a file into a list, the result turns out to be another list wherein each of the elements is a line of your file. Consider you have a file that contains two lines “First line\nSecond Line”. The resulting output of the split function will be [ “First Line”, “Second line”]. You can perform a file split using the Python in-built function splitlines(). 

Consider you have a file named “sample.txt” which contains two lines with two strings in each line respectively – “Hi there”, “You are learning Python”. 

An example of splitting “sample.txt” into a list is shown below:

f = open(“sample.txt”, “r”) 
info = f.read() 
print(info.splitlines()) 
f.close() 

The output of the above code is as follows: 

[‘Hi there’, ‘You are learning Python’] 

We have a file “sample.txt” which is opened in read (“r”) mode using the open() function. Then, we have called f.read() which returns the entire file as a string. The splitlines() function is implemented and it splits the file into two different substrings which are the two lines contained in “sample.txt”. 

4. Splitting a String by newline character (\n) 

You can split a string using the newline character (\n) in Python. We will take a string which will be separated by the newline character and then split the string. The newline character will act as the separator in the Python Split function.  

An example of splitting a string by newline character is shown below: 

str = “Welcome\nto\nPython\nSplit” 
print(str.split(‘\n’)) 

The output of the above code is as follows: 

[‘Welcome’, ‘to’, ‘Python’, ‘Split’] 

Here, we have declared a variable str with a string that contains newline characters (\n) in between the original string.The Split function is implemented with “\n”  as the separator. Whenever the function sees a newline character, it separates the string into substrings.  

You can also perform split by newline character with the help of the splitlines() function. 

5. Splitting a String by tab (\t) 

Tabs are considered as escape characters “\t” in text (.txt) files. When we split a string by tabs, the Split function separates the string at each tab and the result is a list of substrings. The escape character “\t” is used as the separator in the Split function. 

An example of splitting a string by tab is shown below:

str = “Python\tis\ta\tscripting\tlanguage” 
print(str.split(“\t”)) 

The output of the above code is as follows: 

['Python', 'is', 'a', 'scripting', 'language'] 

Here, the variable str is declared with a string with tabs (“\t”). The Split function is executed with “\t” as the separator. Whenever the function finds an escape character, it splits the string and the output comes out to be a list of substrings. 

6. Splitting a String by comma (,) 

We can also split a string by commas (“,”) where commas act as the delimiter in the Split function. The result is a list of strings that are contained in between the commas in the original string.  

An example of splitting a string by commas is shown below: 

str = “Python,was,released,in,1991” 
print(str.split(“,”)) 

The output of the above code is as follows: 

['Python', 'was', 'released', 'in', '1991'] 

Here, the variable str is declared with a string with commas (“,”)  in between them. The Split function is implemented with “,”  as the separator. Whenever the function sees a comma character, it separates the string and the output is a list of substrings between the commas in str. 

7. Splitting a String with multiple delimiters 

You can split a string using multiple delimiters by putting different characters as separator in the Split function. A delimiter is one or more characters in a sequence that are used to denote the bounds between regions in a text. A comma character (“,”) or a colon (“:”) is an example of a delimiter. A string with multiple delimiters can be split using the re.split() function. 

An example of splitting a string with multiple delimiters is shown below:

import re 
str = 'Python\nis; an*easy\nlanguage' 
print(re.split('; |, |\*|\n',str)) 

The output of the above code is as follows: 

['Python', 'is', 'an', 'easy', 'language'] 

In the example above, we import the built-in module re which imports the libraries and functions of Regular Expressions. The variable str is declared with a string with multiple delimiters like newline (\n), semicolon (;), or an asterisk (*). There.split() function is implemented with different delimiters as separator and the output is a list of strings excluding the delimiters.  

8. Splitting a String into a list 

When you split a string into a list around a delimiter, the output comes out to be a partitioned list of substrings. You can take any delimiter as a separator in the Split function to separate the string into a list. 

An example of splitting a string into a list is shown below:

str = “New York-Texas-Colombia” 
print(str.split(“-”)) 

The output of the above code is as follows: 

['New York', 'Texas', 'Colombia'] 

The variable str is declared with a string with dash characters( - ) in between and the Split function is executed with a dash ( - )  as the separator. The function splits the string whenever it encounters a dash and the result is a list of substrings.

Also ReadPython Program to Convert List to String 

9. Splitting a String by hash (#) 

You can also split any string with a hash character (#) as the delimiter. The Split function takes a hash (#) as the separator and then splits the string at the point where a hash is found. The result is a list of substrings.  

An example of splitting a string using a hash is shown below: 

str = “Python#isa#multi-purpose#language” 
print(str.split(“#”)) 

The output of the above code is as follows: 

['Python', 'is a', 'multi-purpose', 'language'] 

The variable str is declared with a string with hash characters( # ) in between them. The Split function is executed with a hash as the separator. The function splits the string wherever it finds a hash  ( # ) and the result is a list of substrings excluding the hash character. 

10. Splitting a String using maxsplit parameter 

The maxsplit parameter defines the maximum number of splits the function can do. You can perform split by defining a value to the maxsplit parameter. If you put whitespaces as separator and the maxsplit value to be 2, the Split function splits the string into a list with maximum two items.  

An example of splitting a string using the maxsplit parameter is shown below:

subjects = “Maths Science English History Geography” 
print(subjects.split(“ ”,2)) 

The output of the above code is as follows: 

['Maths', 'Science', 'English History Geography'] 

Here, you can see the variable str is declared with a string of different subject names. The Split function takes whitespace (“ ”) as a separator and the maximum number of splits or maxsplit is 2. The first two strings “Maths” and “Science” are split and the rest of them are in a single string. 

11. Splitting a String into an array of characters 

You can separate a string into an array of characters with the help of the list() function. The result is a list where each of the element is a specific character.  

An example of splitting a string into an array of characters  is shown below: 

str = “PYTHON” 
print(list(str)) 

The output of the above code is as follows:

['P', 'Y', 'T', 'H', 'O', 'N'] 

Here, the variable str is a string. The string is separated into individual characters using the list() function and the result is a list of elements with each character of the string. 

Also Read: Difference Between Array and List: Python's Data Duel

12. Splitting a String using substring 

You can obtain a string after or before a specific substring with the split() function. A specific string is given as the separator in the Split function and the result comes out to be the strings before and after that particular string.

An example of splitting a string using substring  is shown below: 

fruits = “Orange Banana Mango Apple Cherry” 
print(fruits.split(“Mango”)) 

The output of the above code is as follows: 

['Orange Banana ', ' Apple Cherry'] 

Here, the variable fruits is a string with names of different fruits. We take the string “Mango” as the separator in the Split function. Whenever the function finds the string “Mango”, it splits the whole string into two substrings – one substring before “Mango” and another substring after “Mango”.

Now that you know the different ways of using the Python split() function, try applying these methods to your own projects. Experiment with various separators and maxsplit values to see what works best for your data. Remember, mastering these techniques makes text processing smoother and more efficient. 

If you're finding it tough to break down complex problems efficiently, check out upGrad’s Data Structures & Algorithms free course to strengthen your foundation and start solving challenges with ease. Start today!

Next, check out some miscellaneous tips on the split function that will help you avoid common pitfalls and write cleaner code.

Miscellaneous Tips on Python Split() Function

Working with the Python split() function can sometimes throw unexpected results your way, whether it’s about handling empty strings, dealing with maxsplit limits, or recombining split data. 

These miscellaneous tips are here to clear up confusion and smooth out those rough edges.

  • Handle Empty Strings Carefully 

When the separator appears consecutively or at the start/end of a string, split() can produce empty strings in the result list. For example, "a,,b,".split(",") returns ['a', '', 'b', '']. If empty strings cause issues in your logic, use a list comprehension to filter them out: 

parts = [p for p in "a,,b,".split(",") if p]

This cleans up your list for easier processing.

  • Remember Default Behavior with No Separator 

Calling split() without any arguments splits on all whitespace and ignores multiple spaces. So "hello world".split() gives ['hello', 'world'] without empty entries. But if you explicitly pass a space " " as a separator, multiple spaces produce empty strings: 

"hello   world".split(" ")

Output: 

['hello', '', '', 'world']

Knowing this prevents unexpected empty elements when parsing user input or text data.

Maxsplit Limits the Number of Splits, Not Result Size 

The maxsplit parameter controls how many splits occur, not the final number of pieces. For example,
 "a,b,c,d".split(",", 2) returns ['a', 'b', 'c,d']. This is useful when you want only the first few fields separated and keep the rest intact, such as parsing CSV data where some columns may contain commas.

  • Use re.split() for Multiple Delimiters

 If your string uses different separators (like commas, semicolons, or spaces mixed together), the Python split() function can’t handle them directly. In those cases, import the re module and use re.split() with a pattern. For example, 

import re  
re.split(r'[;, ]+', 'one;two, three four')  

Output:

['one', 'two', 'three', 'four']

This approach lets you flexibly parse complex strings without writing multiple split calls.

  • Converting Split Results Back to String 

After splitting, you might want to join the parts back together—for example, after filtering or processing. Use the join() method:

parts = "a,b,c".split(",")  
cleaned = [p for p in parts if p != 'b']  
result = ",".join(cleaned)  

Output:

'a,c'

This helps maintain clean, readable output and keeps your data consistent.

  • Force Non-String Inputs to String Before Splitting 

The Python split() function only works on strings. If you get input that might be another type (like numbers or None), convert it first to avoid errors:
value = 12345  

str_value = str(value)  
print(str_value.split('2'))  

Output: 

['1', '345']

This tip ensures your code doesn’t break unexpectedly and handles diverse input gracefully.

When you’re ready to level up, explore advanced topics like regular expressions for complex pattern matching, text parsing with the re module, and working with Python’s csv and json libraries for structured data. Keep practicing the Python split() function and these new skills to write cleaner, more powerful code.

Conclusion

The Python split() function breaks a string into smaller pieces based on a separator, making it easier to handle text data. But when strings get complex, with multiple delimiters or irregular spacing, it can get tricky to parse them correctly. Handling these cases without the right tools can slow you down and make your code messy. 

To help bridge this gap, upGrad’s personalized career guidance can help you explore the right learning path based on your goals. You can also visit your nearest upGrad center and start hands-on training today!

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.

Reference:
https://www.pythonweekly.com/p/python-weekly-issue-694-april-10-2025

Frequently Asked Questions (FAQs)

1. How does the Python split() function handle Unicode characters in strings?

2. Can the Python split() function be used to split strings in multiple languages simultaneously?

3. Is there a way to split a string from the right side using Python split()?

4. How does split() handle splitting strings with overlapping separators or patterns?

5. Can split() be used to split strings containing emojis or other special symbols?

6. How can split() be integrated with data cleaning workflows?

7. Does split() affect memory usage when working with very large strings?

8. How can the split() function be used for parsing logs with irregular spacing?

9. Can split() help in tokenizing natural language text for NLP tasks?

10. Is it possible to split strings conditionally based on substring length or content?

11. How does split() behave when used inside list comprehensions or loops?

Rohit Sharma

763 articles published

Rohit Sharma shares insights, skill building advice, and practical tips tailored for professionals aiming to achieve their career goals.

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

17 Months

upGrad Logo

Certification

3 Months