The split() function in Python is used to break a string into a list of substrings based on a specified separator, with the default separator being whitespace. It can also take an optional maxsplit parameter to limit the number of splits. Examples demonstrate its use with and without explicitly defined separators.
What is split()function? The split() method breaks up a string at the specified separator. The syntax of split() is: String_name_variable. ([separator [, maxsplit]]) Both separator and maxsplit are optional
3.
What is separator? Itis a delimiter. The string splits at the specified separator. If the separator is not specified, any whitespace (space, newline etc.)in a string is a separator.
4.
What is maxsplit? Itdefines the maximum number of splits. The default value of maxsplit is -1, meaning, no limit on the number of splits.
5.
How split() worksin Python? take one example: If We Store a sentence given below in a string variable: “Welcome to the python”
6.
If we gothrough the string given below: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n value starts from 0 for first character But we need to break the words from the s
7.
Use of split()without any separator or maxsplit: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n If We use only: split() Example: nm=“Welcome to the python” print(nm.split()) ------output--------- [‘Welcome’,’to’,’the’,’python’] If the separator is not specified space work as separator
8.
Use of split()with any separator: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e ; t o ; t h e ; p y t h o n If We use only: split(“;”) Example: nm=“Welcome;to;the;python” print(nm.split(“;”)) ------output--------- [‘Welcome’,’to’,’the’,’python’] If the separator is specified its break according to those separator mentioned
9.
Use of split()with any separator and maxsplit: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e ; t o ; t h e ; p y t h o n If We use only: split(“;”,2) Example: nm=“Welcome;to;the;python” print(nm.split(“;”,2)) ------output--------- [‘Welcome’,’to’,’the;python’] If the separator is specified and maxsplit given then its break according to maxsplit
10.
Let Us learnmore about split() use: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n How We use split() and store output in another variable. Then What happens???? Let us understand with the help of example:
11.
First thing thatwe need to know: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n That when we declare a variable and store string. nm=“Welcome to the python” String character when we want to read Its read according to index value character by character.
12.
But what happenswhen we use method split(): 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n print(nm.split()) nm=“Welcome to the python” It’s break the words according to the separator, by default separator is spaces. [‘Welcome’,’to’,’the’,’python’]
13.
But what happenswhen we use method split(): 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n [‘Welcome’,’to’,’the’,’python’] break the words from the string and display output as given below:IT Now How we access these words that break using split()
14.
Lets first storethe result using split() into another variable. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n [‘Welcome’,’to’,’the’,’python’] Now let’s store the string using split() into another variable. nm=“Welcome to the python” wstr=nm.split() Now when we use print() to display wstr print(wstr)
15.
Now if youwant to display words of your choice. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n If we want to display “the” Welcome Example: If we want to display On the basis of our choice How we display it. Word one by oneto the python
16.
First thing isto know what happens to a string, after we use split() function. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 W e l c o m e t o t h e p y t h o n Welcome Before we use split() string looks like as shown below: After we use split() its break and store as it shown below: How it is store in memory, if we go through the output. to the python[ ], , ,wstr=
17.
On the basisof output, it store words after split() in memory as shown below. Welcome to the python[ ], , , 0 1 2 3 Welcome Store at 0 index values to Store at 1 index values the Store at 2 index values python Store at 3 index values wstr=
18.
If we wantto display “c” from the word “Welcome”. Welcome It means the 4th character from the word. to the python[ ], , , 0 1 2 3 So Before we move forward let Us understand Understand how the words store in memory after using split() logically. wstr=
19.
How these wordsstore logically. Welcome 0,1,2,3 are the index values as shown below: So how we display the character from each word to the python[ ], , , 0 1 2 3 Now let us understand with an easy method. wstr=
20.
So first thingwe need to understand that when we split() the string Welcome It is store in a variable as shown below: To make it more easy to understand to the python[ ], , , 0 1 2 3 wstr=
21.
Welcome to thepython[ ], , , 0 1 2 3 To make it more easy to understand columns rows 0 1 2 3 4 5 6 0 W e l c o m e 1 t o 2 t h e 3 p y t h o n So 0 is for first row So 1 is for Second row So 2 is for third row So 3 is for fourth row wstr=
22.
Welcome to thepython[ ], , , 0 1 2 3 Example: If we want to display “Welcome” columns rows 0 1 2 3 4 5 6 0 W e l c o m e 1 t o 2 t h e 3 p y t h o n So 0 is for first row So 1 is for Second row So 2 is for third row So 3 is for fourth row wstr= Welcome
23.
Welcome to thepython[ ], , , 0 1 2 3 Example: If we want to display “c” from a “Welcome” columns rows 0 1 2 3 4 5 6 0 W e l c o m e 1 t o 2 t h e 3 p y t h o n So 0 is for first row So 1 is for Second row So 2 is for third row So 3 is for fourth row wstr=
24.
Welcome to thepython[ ], , , 0 1 2 3 columnsrows 0 1 2 3 4 5 6 0 W e l c o m e 1 t o 2 t h e 3 p y t h o n So 0 is for first row So 1 is for Second row So 2 is for third row So 3 is for fourth row wstr= So how we display “c” from a “Welcome” print(wstr[ ]0 ) Welcome So this part is for row number.
25.
So if wewant to display “c” from the word “Welcome” print(wstr[ ]0 ) columns rows 0 1 2 3 4 5 6 0 W e l c o m e 1 t o 2 t h e 3 p y t h o n We need row number with column number also. [ ]3 So the syntax is: variablename[RowNo][ColNo] So we add one more [ ] brackets with wstr
26.
So lets takesome more examples: print(wstr[ ][ ]) columns rows 0 1 2 3 4 5 6 0 W e l c o m e 1 t o 2 t h e 3 p y t h o n Display “python” first character “p” Display “the” 3rd character “e” print(wstr[ ][ ]) 3 0 2 2 Ans: p Ans: e
27.
So you mustknow that if string is : “Hello now you know how to use it in python program” Variable nm store the string given below: If you use split() wstr=nm.split() And use print: print(wstr) ----OUTPUT----- [‘Hello’,’now’,’you’,’know’,’how’,’to’,’use’,’it’,’in’,’python’,’program’]