Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

The split() Function in Python

Python supports object-oriented programming and has a concise, readable, and easy-to-learn syntax. It is no wonder that it is one of the most popular programming languages. An integral part of Python are its built-in functions. You would have surely used some of these functions in your programs. 

We've written a series of articles to help you learn and brush up on the most useful Python functions. In this article, we’ll learn about Python's split() function and how to use it.

If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Python Exit commands, and Type and Isinstance In Python for more content on Python coding interview preparation.

Having trained over 10,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

In this article, we’ll cover:

  • What is the split() Function in Python, and What Does It Do?
  • Syntax for split() Function 
  • The split() Function in Python: Example 
  • FAQs on split() Function in Python

What Is the split() Function in Python and What Does It Do?

The split() function in Python operates on strings. It takes a string as input and splits it wherever it encounters a “separator” (a character that acts as a marker for the split). The output is a list of strings, where the elements are the individual parts of the input string after the splits. 

By default, split() assumes the separator to be whitespace (“ ”). So if we call the split() function for a normal English sentence, the output would be a list of words of that sentence. Suppose the input is a string of words separated by commas (“,”), and we specify that the separator should be the comma character. In that case, the output will be a list where the elements are the individual words of the input string.

Syntax for split() Function 

string.split(separator, maxsplit)

  • string is the string or the name of the string variable that we want to split (input).
  • separator (optional) is the character at which the input string splits.
  • maxsplit (optional) is the maximum number of splits that can be performed on the input string. If the separator appears more times than the value of maxsplit, then we cut the input string as many times as the value of maxsplit. After that, the rest of the string becomes one single list element in the output list.

The split() Function in Python: Example 

Here, we take a look at how you can use the Python function split() next time you need it:

Code

txt = "John and Paul formed a band"

split1 = txt.split()

split2 = txt.split("and")

split3 = txt.split(" ", 3)


print("split1 looks like")

print(split1)

print("\nsplit2 looks like")

print(split2)

print("\nsplit3 looks like")

print(split3)


txt = "John,Paul,George,Ringo formed a band"

split4 = txt.split(",")

print("\nsplit4 looks like")

print(split4)

Output

split1 looks like

['John', 'and', 'Paul', 'formed', 'a', 'band']


split2 looks like

['John ', ' Paul formed a b', '']


split3 looks like

['John', 'and', 'Paul', 'formed a band']


split4 looks like

['John', 'Paul', 'George', 'Ringo formed a band']

Found this article useful? You can learn about more Python functions in our learn folder. 

FAQs on split() Function in Python

Q1. Can the input to the split() function in Python only be a string?

Yes. Since it is a string method, split() can only be invoked on strings.

Q2. Can the separator in Python’s split() function be a word?

Absolutely! The separator just needs to be a string. "A and B".split("and") will return ['A ', ' B']

Q3. For the split() function in Python, what will the data type of the individual elements of the output array be?

The individual elements in the list will always be strings themselves.

Q4. Can the separator in Python’s split() function be a number?

Yes and no. For example, the separator can’t be the integer 2, but the separator can be the character “2”. For example, "1232425262".split("2") will return ['1', '3', '4', '5', '6', ''].

Ready to Nail Your Next Coding Interview?

Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts