Programs

Python String split() Method [With Examples]

String manipulation is an important activity in all programming endeavours, for numerous reasons. Traditionally, programmers had to manually write the string manipulations methods and invoke them when needed.

However, as programming languages got more versatile, methods were added to them to save the programmer’s time. These methods are essential to perform the basic tasks often needed in any programming environment. For example, in the case of Python, you get several built-in functions to perform various tasks. Plus, you can create your custom functions, too. 

In this article, we’ll be looking at one such method available in Python for string manipulation: the split() method. But before we get into the working and example of the split() method, let’s first understand the basics of Strings in Python. 

Check out our data science courses to upskill yourself.

Explore our Popular Data Science Courses

Basics of Strings in Python

String data type in Python, abbreviated as ‘str’, holds the text type data. Here’s an example to understand Strings better: 

my_name = “Ron”

print(type(my_name))

We can also use single quotes instead of double quotes, as follows: 

my_name = ‘Ron’

print(type(my_name))

Another way to declare a String variable in Python, by setting it explicitly, goes as follows: 

my_name= str(“Ron”)

print(type(my_name))

The print() method in all three examples will return the same results as below: 

Output:

<class ‘str’>

<class ‘str’>

<class ‘str’>

This shows that all three variables are of ‘str’ type, regardless of how we have defined the variable. Now that we have Strings in place, it’s important to realise that sometimes, we need to perform various manipulations or modifications on these strings. For that, Python provides built-in string methods. Some of the more commonly used string methods include: 

  • Replace()
  • Strip()
  • Split()
  • Join()
  • Capitalize()
  • Casefold/Lower()
  • Count()
  • Endswith()
  • Startswith()
  • Index(), and more. 

Here’s a list of all the String methods available in Python. 

This article will look at the split() method in detail, so let’s get started with that! 

String split() method in Python

Python’s split() method breaks a large string into smaller chunks of substrings. Unlike concatenation, which merges different strings, the split method breaks one string into different sections. The split() method divides the string into substrings and adds the data to an array with the help of the defined separator. If the programmer doesn’t define a separator while calling the split() method, the white space is used by default as the separator. 

Let’s understand the working of the String split() method in Python from the very basics for the uninitiated. Suppose we have the following input – “A; B; C”. The split() method will break this input into an array with three elements, as follows: 

[“A”, “B”, “C”]

The punctuation marks in our initial input (semi-colons) are the separators or delimiters in this case. For example, ‘,’, ‘;’, ‘@’, ‘&’, ‘:’, ‘(‘, ‘>’ etc., all these are delimiters. 

Parameters of String split() method

The split() method in Python takes the following parameters: 

  • Separator – This is the delimiter at which the string split occurs. If this is not mentioned, the split() method breaks the strings based on whitespaces. 
  • Maxsplit – This refers to the maximum number of splits. If this isn’t specified, the method does not take any value, and there is no limit on the number of possible splits. The default value of maxsplit is ‘-1’, which refers to all occurrences

Here is the syntax of the split() method using both the parameters: 

str.split(separator, maxsplit)

Now that you’ve understood the split() method and the parameters it takes, let’s look at the situations where you will need this method. 

Top Data Science Skills to Learn in 2022

The requirement of the split() function in Python

The split() function is useful in several cases where you require string manipulation. Some of those cases are when we need to perform splitting based on: 

  • The delimiter space, comma, tab, and so on
  • Multiple delimiters
  • Occurrence of characters
  • File into a list
  • String into a character array
  • File manipulation activities
  • Substrings from the given string as the delimiter, etc. 

Let’s now look at some hands-on examples of using the split() function in various cases.

Python String split() method – Examples

Example 1 – Splitting the string using space as the delimiter: 

data = “upGrad is here for you”

splitdata = data.split()

print(splitdata)

Running the above code will give the following output: 

[‘upGrad’, ‘is’, ‘here’, ‘for’, ‘you’]

Example 2 – Splitting the string using a comma as the delimiter: 

data = “upGrad, Data Science, Data Analytics, Big Data”

splitdata = data.split(‘,’)

print(splitdata)

The output will be as follows: 

[‘upGrad’, ‘Data Science’, ‘Data Analytics’, ‘Big Data’]

Example 3 – Splitting the string using hash as the delimiter:

data = “#upGrad#Data Science#Data Analytics#Big Data”

splitdata = data.split(“#”)

print(split)

The output will be as follows: 

[‘upGrad’, ‘Data Science’, ‘Data Analytics’, ‘Big Data’]

Example 4 – Splitting the data in a given file into a list (using splitlines()):

Supposing our text file had the following content – “This is the first line of the file. Welcome to upGrad!”. 

#opening a file in read mode using open() function

fp = open(“example.txt”, “r”)

#reading the contents of the file 

fr = fp.read()

Displaying the content of the file as a list

print(fr.splitlines())

fp.close()

The output of this program is as follows: 

[‘This is the first line of the file’, ‘Welome’, ‘To’, ‘upGrad’]

Example 5 – Splitting a given string into several characters with one of the substrings being the delimiter:

str_to_split = “upGrad, is, for, you”

splitstring = str_to_split(“is”)

print(splitstring)

The output of the above program will be: 

[‘upGrad, ‘, ’ ‘, for’ ‘, you’]

In Conclusion

We hope this article helped you with understanding and getting hands-on with the String split() method available in Python. Like split(), there are various other methods, too, that can be used as easily as this one. All you need to know is the correct scenario where you need to invoke which method, and then it’s all straightforward and intuitive.

These methods give the power and versatility to the Python programming language that makes it one of the most sought after languages today. No wonder the language is handy in so many varied contexts – from software development to data analytics, data visualisation to statistical computations, and so much more.

If you’d want to dive deeper into working with Python, especially for data science, upGrad brings you the Executive PGP in Data Science. This program is designed for mid-level IT professionals, software engineers looking to explore Data Science, non-tech analysts, early career professionals, etc. Our structured curriculum and extensive support ensure our students reach their full potential without difficulties.  

1. What is the split() method in Python?

The split() method is one of the vital string manipulation methods available in the Python programming language.

2. How many parameters does the split() method in Python take?

The split() method takes two parameters: Separator Maxsplit

3. What are the default values of the parameters of the split() method?

The default values are as follows: For separator: the default is the whitespace. For maxsplit: the default is ‘-1’, which refers to all values.

Want to share this article?

Become a Certified Data Scientist

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks