string concepts in python part 5

By
There are some operational functions for the strings as given below:-
1.) split() :-The split() function is used to split(breaks up) at a specified separator and returns a list of strings. 
Syntax
string.split(separator,maxsplit)
The split function takes maximum two parameters as given below:-
  • Separator:-This is an optional parameter.If separator is specified ,the string split at this specified separator.If separator is not specified ,the string is split at white space (space,newline,coma etc.) separator.
  • maxsplit:-This is also an optional parameter.The maxsplit is used to define maximum number of splits. The default value of max split is -1 ,means no limit of number of splits.
e.g.
str1='Welcome to MNT LAB'
a=str1.split()
print("split at space: ",a)
str2="ram,shyam,mohan,rameh,neha"
b=str2.split(',')
print("split at comma(','): ",b)
c=str2.split('.')
print("split at dot('.'): ",c)
d=str2.split(',',2)
print("2 maxsplit at comma(','): ",d)
e=str2.split(',',3)
print("3 maxsplit at comma(','): ",e)
f=str2.split(',',0)
print("0 maxsplit at comma(','): ",f)
g=str1.split(' ',2)
print("2 maxsplit at space(' '): ",g)
Output of the Above Python Codes:-
split at space:  ['Welcome', 'to', 'MNT', 'LAB']
split at comma(','):  ['ram', 'shyam', 'mohan', 'rameh', 'neha']
split at dot('.'):  ['ram,shyam,mohan,rameh,neha']
2 maxsplit at comma(','):  ['ram', 'shyam', 'mohan,rameh,neha']
3 maxsplit at comma(','):  ['ram', 'shyam', 'mohan', 'rameh,neha']
0 maxsplit at comma(','):  ['ram,shyam,mohan,rameh,neha']
2 maxsplit at space(' '):  ['Welcome', 'to', 'MNT LAB']
2.) rsplit() :-The split() function is used to split from the right at a specified separator and returns a list of strings. 
Syntax
string.rsplit(separator,maxsplit)
The rsplit function takes maximum two parameters as given below:-
  • Separator:-This is an optional parameter.If separator is specified ,the string split from the right at this specified separator.If separator is not specified ,the string is split from right at white space (space,newline,coma etc.) separator.
  • maxsplit:-This is also an optional parameter.The maxsplit is used to define maximum number of splits. The default value of max split is -1 ,means there 
e.g.
str1='Welcome to MNT LAB'
a=str1.rsplit()
print("split at space: ",a)
str2="ram,shyam,mohan,rameh,neha"
b=str2.rsplit(',')
print("split at comma(','): ",b)
c=str2.rsplit('.')
print("split at dot('.'): ",c)
d=str2.rsplit(',',2)
print("2 maxsplit at comma(','): ",d)
e=str2.rsplit(',',3)
print("3 maxsplit at comma(','): ",e)
f=str2.rsplit(',',0)
print("0 maxsplit at comma(','): ",f)
g=str1.rsplit(' ',2)
print("2 maxsplit at space(' '): ",g)
h=str2.rsplit(',',1)
print("1 maxsplit at comma(','): ",h)
Output of the Above Python Codes:-
split at space:  ['Welcome', 'to', 'MNT', 'LAB']
split at comma(','):  ['ram', 'shyam', 'mohan', 'rameh', 'neha']
split at dot('.'):  ['ram,shyam,mohan,rameh,neha']
2 maxsplit at comma(','):  ['ram,shyam,mohan', 'rameh', 'neha']
3 maxsplit at comma(','):  ['ram,shyam', 'mohan', 'rameh', 'neha']
0 maxsplit at comma(','):  ['ram,shyam,mohan,rameh,neha']
2 maxsplit at space(' '):  ['Welcome to', 'MNT', 'LAB']
1 maxsplit at comma(','):  ['ram,shyam,mohan,rameh', 'neha']
3.) count() :-This function counts the number of occurrence of substring in a given string between start and end index. 
Syntax:-
string.count(substring, start , end)
This function takes 3 arguments as given below:-
  • substring:-It is the sub part of any string.
  • start:-It is the starting point(index) of any string where search starts. By default start index value=0,because index in python start with 0 .It is an optional parameter.
  • end:-It is the ending point of any string where search ends.
e.g.
#type1
str1='Welcome to MNT LAB,MAT LAB,CAT LAB'
substring='LAB'
a=str1.count(substring)
print("Number of count of substring present in str1=",a)
#type2
str2='welcome to my home'
substring='o'
b=str2.count(substring)
print("Number of count of substring present in str2=",b)
#type3
str3='Delhi is the capital of india'
c=str3.count('i')
print("Number of count of substring present in str3=",c)
#type4
str4='Lucknow is the capital of uttar pradesh'
substring='a'
d=str4.count(substring,7,35)
e=str4.count('a',0,30)
print("Number of count of substring present in str4=",d)
print("Number of count of substring present in str5=",e)
Output of the Above Python Codes:-
Number of count of substring present in str1= 3
Number of count of substring present in str2= 3
Number of count of substring present in str3= 5
Number of count of substring present in str4= 4
Number of count of substring present in str5= 3
4.) partition() :-The partition method searches the specified string(substring), and splits the string into a tuple containing three elements.
The partition method always returns 3 tuple containing from given string. The specified string(substring) will be always middle tuple between 3 tuple.
  • If specified string(substring) is found in the given string then split the string into a tuple containing three elements and specified string will be always middle elements between three tuple.
  • If specified string(substring) is not found in the given string then split the string into a tuple containing 3 elements.First tuple is whole given string,second and third tuple will be empty string.
Syntax:-
string.partition(substring)
e.g.
str='Welcome to MNT LAB.'
#type1
a=str.partition('to')
print('partition1=',a)
#type2
b=str.partition('MNT')
print('partition2=',b)
#type3
c=str.partition('LAB')
print('partition3=',c)
#type4
d=str.partition('Welcome')
print('partition4=',d)
#type5
e=str.partition('do')
print('partition5=',e)
#type6
f=str.partition('m')
print('partition6=',f)
#type7
g=str.partition('.')
print('partition7=',g)
Output of the Above Python Codes:-
partition1= ('Welcome ', 'to', ' MNT LAB.')
partition2= ('Welcome to ', 'MNT', ' LAB.')
partition3= ('Welcome to MNT ', 'LAB', '.')
partition4= ('', 'Welcome', ' to MNT LAB.')
partition5= ('Welcome to MNT LAB.', '', '')
partition6= ('Welco', 'm', 'e to MNT LAB.')
partition7= ('Welcome to MNT LAB', '.', '')
5.) rpartition() :-The rpartition() method splits the given string at the last occurrence of the specified string(substring) and returns a tuple containing 3 elements. It takes one arguments like partition method.
  • If specified string(substring) is found in the given string then split the string into a tuple containing three elements and specified string will be always middle elements between three tuple.
  • If specified string(substring) is not found in the given string then split the string into a tuple containing 3 elements.First tuple is whole given string,second and third tuple will be empty string.
Syntax:-
string.rpartition(substring)
e.g.
str='Welcome to MNT LAB LAB.'
#type1
a=str.rpartition('to')
print('rpartition1=',a)
#type2
b=str.rpartition('MNT')
print('rpartition2=',b)
#type3
c=str.rpartition('LAB')
print('rpartition3=',c)
#type4
d=str.rpartition('Welcome')
print('rpartition4=',d)
#type5
e=str.rpartition('do')
print('rpartition5=',e)
#type6
f=str.rpartition('m')
print('rpartition6=',f)
#type7
g=str.rpartition('.')
print('rpartition7=',g)
Output of the Above Python Codes:-
rpartition1= ('Welcome ', 'to', ' MNT LAB LAB.')
rpartition2= ('Welcome to ', 'MNT', ' LAB LAB.')
rpartition3= ('Welcome to MNT LAB ', 'LAB', '.')
rpartition4= ('', 'Welcome', ' to MNT LAB LAB.')
rpartition5= ('', '', 'Welcome to MNT LAB LAB.')
rpartition6= ('Welco', 'm', 'e to MNT LAB LAB.')
rpartition7= ('Welcome to MNT LAB LAB', '.', '')
6.) join() :-The join function (method) takes all items in a iterables and joins them into specified string(separator).Iterable means ,any iterable object where all the returned values are strings.
Return value from join method 
The join function returns a string generated by joining the elements of an iterable by string separator. Iterable native type :list,tuple ,set ,dictionary etc.
Syntax:-
string.join(iterable)
e.g.
#use join method in two string
s1='welcome '
s2='MNT '
s=s1.join(s2)
print("joining s2 string in s1 separator:",s)
#joing with empty string
str='A Truth Learning'
s1=' '
s=s1.join(str)
print("joining str string in s1 separator:",s)
#joining list of strings to a separator(-)
list1=['a','b','c','d']
s1='-'
s=s1.join(list1)
print("joining list of strings to a separator(-)",s)
list2=['1','2','3','4','5']
s1='.'
s=s1.join(list2)
print("joining list of numeric strings to a separator(.)",s)
#joining tuple of strings to a separator(>>)
tuple1=('P','Q','R','S','T')
s1='>>'
s=s1.join(tuple1)
print("joining tuple of strings to a separator(>>)",s)
#use join method with sets
set1={'1','3','5','7','9'}
s1=';'
print("join method with sets:",s1.join(set1))
#use join method with dictionary
dic1={'python':1,'c#':2,'java':3,'php':4,'c':5}
s='->'
print("join method with dictionary:",s.join(dic1))
Output of the Above Python Codes:-
joining s2 string in s1 separator: Mwelcome Nwelcome Twelcome  
joining str string in s1 separator: A   T r u t h   L e a r n i n g
joining list of strings to a separator(-) a-b-c-d
joining list of numeric strings to a separator(.) 1.2.3.4.5
joining tuple of strings to a separator(>>) P>>Q>>R>>S>>T
join method with sets: 3;9;7;5;1;
join method with dictionary: python->c#->java->php->c
For More...
Watch Complete Lecture Video:

0 comments:

Post a Comment

Powered by Blogger.