Learn How To Use Split Function In Python

Swetha Ravindranath
Edureka
Published in
4 min readJul 18, 2019

Python programming language has various data types including strings. Even though strings are immutable in nature, we can still manipulate a string using functions like a split function. It breaks down larger strings into smaller strings using different parameters. In this article, we will learn about how we can use the split function in python. Following are the topics discussed in this article:

  • What Is A String?
  • Need For Split Function?
  • How To Use Split Function In Python?
  • Split Parameters
  • Separator
  • Max
  • Example

What Is A String?

Strings in python represent unicode character values. Python does not have a character data type, a single character is also considered as a string.

We use the single or double quotes to declare a string. To access a string, we use the indexes and square brackets. Since strings are mutable in nature, we cannot make any changes after declaring a string.

name = "Edureka"print(name[0])Output: E

Although we cannot change a string after declaration, we can split a string in python.

Need For Split Function

Split function returns a list of strings after dividing the string based on the given separator. Following are the advantages of using a split function in python:

  • At some point we may have to break down a large string into smaller chunks or strings.
  • It is the opposite of concatenation, which adds two strings together.
  • The white spaces are considered as a separator if none is provided in the split function.
  • It becomes easier to analyze and deduct conclusions.
  • It helps to decode encrypted strings.

How To Use Split Function In Python?

Split function breaks down a larger string and gives a list with smaller chunks or strings. Below is an example to split a string in python.

a = "We are Edureka, we have cutting edge tutorials and certification programs to upskill your knowledge"print(a.split())Output: [ 'We' , 'are' , 'Edureka' , 'we' , 'have' , 'cutting' , 'edge' , 'tutorials' , 'and' , 'certification' , 'programs' , 'to' , 'upskill' , 'your' , 'knowledge']

Above is a simple example to show how split function can be used to break down the whole text into smaller strings. But split function has different parameters to optimize the execution.

Split Parameters

  1. Separator — It acts like a delimiter, the string is broken down according to the separator specified. It is optional as well, if there is no separator specified, the default separator will be the white space.
  2. Max — It is optional as well. It defines the number of splits that will take place. The default value is -1 which means no limits on the number of splits.

Separator

Below is an example to show the split function with a separator parameter:

a = "Edureka is the biggest edtech company, it has many cutting edge courses to learn"print(a.split(" , ")b = "Sunday*Monday*Tuesday*Wednesday*Thursday*Friday*Saturday"print(a.split(" * ")Output: [ ' Edureka is the biggest edtech company' , 'it has many cutting edge courses to learn' ]
['Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ]

In the above example, the separator is specified according to which the string is split into smaller strings.

Max

Below is an example to show the split function with a max parameter:

a = "my*name*is*python"print(a.split(" * " , 3)Output : [ 'my' , 'name' , 'is' , 'python' ]

The max parameter in the above example is set to 3, which means the output will have 4 elements in the list of strings.

Example

Below are a few examples, where we can use the split function to split the string into smaller chunks or strings.

a = "my name is python"print(a.split())b = "CatDogAntCarTap"print([b[ i : i+3] for i in range(0 , len(b) , 3)])c = "python#was#made#by#Guido#van#rossum"print(c.split(" #", 6)d = " this , will , be , in , output, this will be not"print(d.split(" , " , 4)Output: [ 'my' , 'name' , 'is' , 'python' ]
['Cat' , 'Dog' , 'Ant' , 'Car' , 'Tap' ]
['python' , 'was' , 'made' , 'by' , 'Guido' , 'van' , 'rossum' ]
['this' , 'will' , 'be' , 'in' , 'output' ]

In this blog, we have learnt how split function can be used to break down large strings into smaller chunks or strings. String is an immutable data structure which means it cannot be changed once you have declared it. Although manipulation can be done using split function. Python programming language has different data types like list, dictionary, tuple, set etc.

Primitive data types and specialized data structures optimize your code and gives an edge to python over other programming languages. To master your skills enroll to Python online training program and kick-start your learning.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Python Tutorial

2. Python Functions

3. File Handling in Python

4. Python Numpy Tutorial

5. Scikit Learn Machine Learning

6. Python Pandas Tutorial

7. Matplotlib Tutorial

8. Tkinter Tutorial

9. Requests Tutorial

10. PyGame Tutorial

11. OpenCV Tutorial

12. Web Scraping With Python

13. PyCharm Tutorial

14. Machine Learning Tutorial

15. Linear Regression Algorithm from scratch in Python

16. Python for Data Science

17. Python Regex

18. Loops in Python

19. Python Projects

20. Machine Learning Projects

21. Arrays in Python

22. Sets in Python

23. Multithreading in Python

24. Python Interview Questions

25. Java vs Python

26. How To Become A Python Developer?

27. Python Lambda Functions

28. How Netflix uses Python?

29. What is Socket Programming in Python

30. Python Database Connection

31. Golang vs Python

32. Python Seaborn Tutorial

33. Python Career Opportunities

Originally published at https://www.edureka.co on July 15, 2021.

--

--