Blog

Python Split Method with Example

  • (5.0)
  •   |   662 Ratings

Introduction:

Python is a powerful programming language that has started regaining its fame for its usage in the Data Science along with the latest technologies like R and etc. Having said that, let us take a look at the tiny winy bits of concepts to get ourselves stronger in this programming language.
In this article, we will try to understand the concept of a split in Python programming language. To provide you a better understanding of what split can be used for in Python programming language, take a look at the following section.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

Python split method:

String splitting is the exact opposite of the concatenation of strings (which merges or combines more than one strings into a single string). This can be achieved by using the split() method provided by the Python programming language. The split() method takes an identifier as an argument and based on the identifier passed to it, the passed string will be split into array of smaller strings. If there is no identifier provided to the method, then by default the white space character is used as the identifier.

Related Page: Comparing R Language Vs Python

Let us now take a look at a small example to see how this can be achieved.

Now the same example, if we do not provide any identifier to the split() method – this is how it is going to behave.

Now if you want to assign these smaller strings to individual string objects rather than returning as a list of strings, this is how you can achieve it. The input string has been split into 3 individual smaller strings and each of those resultant smaller strings are assigned to 3 different string variables that we can use as follows:

This comes in very handy when you get yourself working with strings and variables extensively. Arrays and lists in Python are the backbones of Python programming language, which comes in handy whatever you might be involved in too.
Now let us take another example, where we try to split an original string and iterate over the split substrings to do some operation. To break it down piece by piece, the split function will split the provided original string into multiple substrings based on the identifier provided to the split() function. Based on that, an array will be provided on which we are going to iterate over and do some operations on it.

What we have done in the above example, is to split the first two occurrences of the identifier mentioned and post which we have opted not to split the input string into smaller strings. Hence you can see the difference on how the output is given based on the identifier and the repetition count.
Now consider the following example, where we try to read out a file that has just the following three lines in a text file named ‘TestInput.txt’ on E: drive.

Frequently Asked Python Interview Questions & Answers

Contents of the input file to the program:

 

Danerys, the Mother of Dragons
Jon, King in the North
Arya, A girl has No Name

Now the Python program is written in such a way that each line of the text file is read and on the line read, we will apply the split() function based on the identifier “,” and assign the Python substrings to a construct. This construct when it is iterated over, we will print each of the members of the construct with a tab space. So the following program is to read the entire file, line by line and each line is split into multiple substrings based on the identifier “,” and each substring of the read line is displayed under the line itself with a tab space.

Isn’t this a small and an easy program to work with in Python?

There is hardly anything that is tough in this example, but if you feel so, then take a moment and grasp whatever you can in the programming language – keep practicing to get the grip over the language. Python is a simple programming language, doesn’t require much time if you already have some programming knowledge and if you don’t have any – it is even better because you will be able to pick up the syntax with no confusion when compared with the other programming languages.

Conclusion:

In this article, we have seen what cases split can be used and why are they used in Python programming language. We have also tried to take a deeper look into the concept with various examples as well.

Hope that you were clear with the concepts after going through this detailed article, please do comment if you have any suggestions to make.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules