String concepts in python Part 4

By
There are some search and replace functions operations in strings as given below:-
1.) find():- The find() function returns the index value of the string where the substring is found between start index and end index.start index and end index is optional parameter.

  • If substring is present in given string then this function returns the index of the first occurrence of the substring.
  • If the substring is not exist inside the string then it returns -1 index
Syntax:-
string.find(substring,start,end)
e.g.
#String.find(substring,start,end)
#type1
str1="Welcome to MNT LAB"
r1=str1.find('MNT')
print("Substring('MNT')is present in str1: ",r1)
#type2
#search starts from string "MNT LAB" 
r2=str1.find('LAB',-8)
print("Substring('LAB')is present in str1: ",r2)
p=str1.find('MNT',0)
print("Substring('MNT')is present in str1: ",p)
#type3
#search from 0 index OR starts with "Welcome to MNT LAB" string
r3=str1.find('msdotnet',0)
print("Substring('msdotnet')is present in str1: ",r3)
#type4
#search from 0 index OR starts with "Welcome to MNT LA",end =-1
r4=str1.find('to',0,-1)
print("Substring('to')is present in str1: ",r4)
#type5
r5=str1.find('Welcome',0,-11)
if(r5 !=-1):
    print("'Welcome' substring is present in str1")
else:
    print("'Welcome' substring is not present in str1")
Output of the Above Python codes:-
Substring('MNT')is present in str1:  11
Substring('LAB')is present in str1:  15
Substring('MNT')is present in str1:  11
Substring('msdotnet')is present in str1:  -1
Substring('to')is present in str1:  8
'Welcome' substring is present in str1
2.) rfind():-The rfind() function returns the highest index of the substring where it is found in the string, if it is not found then it returns -1The rfind function traverses the string from backward direction.
Syntax:-
string.rfind(substring,start,end)

e.g.
#String.rfind(substring,start,end)
#type1
str1="Welcome to MNT LAB to MNT LAB"
r1=str1.rfind('MNT')
print("Substring('MNT')is present in str1: ",r1)
#type2
#search starts from string "MNT LAB" 
r2=str1.rfind('LAB',-8)
print("Substring('LAB')is present in str1: ",r2)
p=str1.rfind('MNT',0)
print("Substring('MNT')is present in str1: ",p)
#type3
r3=str1.rfind('msdotnet',8)
print("Substring('msdotnet')is present in str1: ",r3)
#type4
r4=str1.rfind('to',0,-1)
print("Substring('to')is present in str1: ",r4)
#type5
r5=str1.rfind('Welcome',0,-11)
if(r5 !=-1):
    print("'Welcome' substring is present in str1")
else:
    print("'Welcome' substring is not present in str1")
Output of the Above Python codes:-
Substring('MNT')is present in str1:  22
Substring('LAB')is present in str1:  26
Substring('MNT')is present in str1:  22
Substring('msdotnet')is present in str1:  -1
Substring('to')is present in str1:  19
'Welcome' substring is present in str1
3.) index():-The index() function returns the index value of a substring inside the string (if it is found).if it is not found then it through an exception.It is same as a find() function.
Syntax:-

string.index(substring,start,end)
e.g.
#type1
str1="The capital of india is delhi"
#substring is searched in whole str1 
r1=str1.index('india')
print("substring('india')in str1:",r1)
#type2
#substring is searched in 'is delhi' 
r2=str1.index('is',20)
print("substring('is')in str1:",r2)
#type3
#substring is searched in 'is delhi' 
r3=str1.index('delhi',-9)
print("substring('delhi')in str1:",r3)
#type4
#substring is searched in 'capital of' 
r4=str1.index('capital',3,13)
print("substring('capital')in str1:",r4)
#type5
#substring is searched in 'tal of india' 
r5=str1.index('of',8,-6)
print("substring('capital')in str1:",r5)
Output of the Above Python codes:-
substring('india')in str1: 15
substring('is')in str1: 21
substring('delhi')in str1: 24
substring('capital')in str1: 4
substring('capital')in str1: 12
4.) rindex():-The Index() function returns the highest index value of the substring inside the string(if it is found).If it is not found then it raised an exception.The rindex function traverses the string from backward direction.
Syntax:-

string.rindex(substring,start,end)
e.g.
#type1
str1="The capital of india is is delhi"
#substring is searched in whole str1 
r1=str1.rindex('india')
print("substring('india')in str1:",r1)
#type2
#substring is searched in 'is delhi' 
r2=str1.rindex('is',20)
print("substring('is')in str1:",r2)
#type3
#substring is searched in 'is delhi' 
r3=str1.rindex('delhi',-9)
print("substring('delhi')in str1:",r3)
#type4
#substring is searched in 'capital of' 
r4=str1.rindex('capital',3,13)
print("substring('capital')in str1:",r4)
#type5
#substring is searched in 'tal of india' 
r5=str1.rindex('of',8,-6)
print("substring('capital')in str1:",r5)
Output of the Above Python codes:-
substring('india')in str1: 15
substring('is')in str1: 24
substring('delhi')in str1: 27
substring('capital')in str1: 4
substring('capital')in str1: 12
5.) replace():-The replace() function is used to replace one value with another.The maximum characters are replaced if maximum value is given.
The replace() function takes maximum 3 parameters.
  • old:-A old substring which you want to replace.
  • new:-It is used to replace old substring in given string.
  • count:-It is an optional parameter.the number of times ,you want to replace the old substring with new substring inside the string.
Syntax:-
string.replace(old substring,new substring,count)
e.g.
msg='Welcome to MNT LAB Welcome to MNT LAB MNT LAB MNT LAB'
#type1
msg1=msg.replace('MNT','MSDOTNET')
print("After replacing value new string : ",msg1)
#type2
msg2=msg.replace('MNT','MSDOTNET',2)
print("After replacing value new string : ",msg2)
#type3
msg3=msg.replace('MNT','MSDOTNET',0)
print("After replacing value new string : ",msg3)
#type4
msg4=msg.replace('MN','DO')
print("After replacing value new string : ",msg4)
#type5
msg5=msg.replace('W',' ')
print("After replacing value new string : ",msg5)
#type6
msg6=msg.replace('LAB','BAL ')
print("After replacing value new string : ",msg6)
Output of the Above Python codes:-
After replacing value new string :  Welcome to MSDOTNET LAB Welcome to MSDOTNET LAB MSDOTNET LAB MSDOTNET LAB
After replacing value new string :  Welcome to MSDOTNET LAB Welcome to MSDOTNET LAB MNT LAB MNT LAB
After replacing value new string :  Welcome to MNT LAB Welcome to MNT LAB MNT LAB MNT LAB
After replacing value new string :  Welcome to DOT LAB Welcome to DOT LAB DOT LAB DOT LAB
After replacing value new string :   elcome to MNT LAB  elcome to MNT LAB MNT LAB MNT LAB
After replacing value new string :  Welcome to MNT BAL  Welcome to MNT BAL  MNT BAL  MNT BAL 
6.) lstrip():-The lstrip() function is used to trim the string from the left.The lstrip() function removes the characters from the left based on the arguments.
Syntax:-
string.lstrip([chars])
  • chars(optional parameter):-it is used to removed set of characters from the string.if this(chars) argument is not provided,then all leading white space are removed from the string.
e.g.
str1='---MNT LAB---'
print("1st:",str1.strip())
print("2st:",str1.strip('-'))
print("3st:",str1.lstrip())
print("4st:",str1.lstrip('-'))
str2=' msdotnet'
print("5st:",str2.lstrip('rst'))
print("6st:",str2.lstrip(' rst'))
print("7st:",str2.lstrip(' mst'))
str3="http://www.msdotnet.co.in"
print("8st:",str3.lstrip('htp:/'))
print("9st:",str3.lstrip('ptp:/'))
Output of the Above Python codes:-
1st: ---MNT LAB---
2st: MNT LAB
3st: ---MNT LAB---
4st: MNT LAB---
5st:  msdotnet
6st: msdotnet
7st: dotnet
8st: www.msdotnet.co.in
9st: http://www.msdotnet.co.in
7.) rstrip():-The rstrip() function is used to trim the string from the right.The lstrip() function removes the characters from the right based on the arguments.
Syntax:-
string.rstrip([chars])
  • chars(optional parameter):-it is used to removed set of characters from the string.if this(chars) argument is not provided,then all leading white space are removed from the string.
e.g.
str1='###MNT LAB###'
print("1st:",str1.strip())
print("2st:",str1.strip('#'))
print("3st:",str1.rstrip())
print("4st:",str1.rstrip('#'))
str2=' msdotnet '
print("5st:",str2.rstrip('rst'))
print("6st:",str2.rstrip(' ret'))
print("7st:",str2.rstrip('mst'))
str3="http://www.msdotnet.co.in//"
print("8st:",str3.rstrip('/'))
Output of the Above Python codes:-
1st: ###MNT LAB###
2st: MNT LAB
3st: ###MNT LAB###
4st: ###MNT LAB
5st:  msdotnet 
6st:  msdotn
7st:  msdotnet 
8st: http://www.msdotnet.co.in
Special Notes:- 
dir() Function:-The dir() function returns a list of valid attribute of the specified object.It returns all built-in properties and methods of a specified object.
Syntax:-
dir(object)
  • Here Object is an optional parameter.It takes maximum one object.
e.g.
#dir() method returns a list of valid attribute of the objectlist1=['a',1,'2','b','c','ram']
a=dir(list1) #takes max one arguments,but is optional
b=dir()
#print(a)
print(" ")
#if object is not passed,it returns the list of names in current local scope
#print(b)
#print(" ")
#user defined
class student:
    def dir1():
        return [Id,Name,Age,Address,Mobile]
val1=student()
print(dir(val1))
Output of the Above Python codes:-
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
 
['__annotations__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'list1']
 
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'dir1']
 
For More...
Watch complete Lecture 16 video:-
python string


0 comments:

Post a Comment

Powered by Blogger.