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 blog:
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
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.
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.
Have any questions? Mention them in the comments, we will get back to you as soon as possible.