Data Science and Machine Learning Internship ...
- 1k Enrolled Learners
- Live Class
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:
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)
Although we cannot change a string after declaration, we can split a string in python.
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:
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.
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.
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.
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.
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.
|Python Certification Training Course|
Class Starts on 17th December,2022
17th DecemberSAT&SUN (Weekend Batch)