STRING OPERATIONS ================== REMOVAL OF SPACE FROM THE STRING ================================== "PYTHON PROGRAMMING" ==> SPACE NEVER REMOVED WHEN THE SPACE IN BETWEEN THE GIVEN DATA " PYTHON" "PYTHON " " PYTHON " ==> REMOVAL OF SPACE FROM THE STRING IS POSSIBLE, WHEN THE STRING HAVE SPACES AT THE BEGINNING OR AT THE ENDING OR BOTH ==> THREE INBUILT METHODS: 1) strip() 2) rstrip() 3) lstrip() 1) strip() ======= Syntax: str_object.strip() ==> we can remove all the spaces of the strings from beginning and from the ending 2) rstrip() ======= ==> strip operation in right side Syntax: str_object.rstrip() ==> we can remove all the spaces of the string from right side only (ending) 3) lstrip() ======= ==> strip operation from left side of the string data Syntax: str_obj.lstrip() str1 = "Python Programming" str2 = " Python" str3 = "Python " str4 = " Python " print("The Given Strings are:") print(str1) print(str2) print(str3) print(str4) str5 = str1.strip() str6 = str2.strip() str7 = str3.strip() str8 = str4.strip() str9 = str2.rstrip() str10 = str3.rstrip() str11 = str4.rstrip() str12 = str2.lstrip() str13 = str3.lstrip() str14 = str4.lstrip() # After the Strip operation print("The Strings are:") print(str5) print(str6) print(str7) print(str8) print(str9) print(str10) print(str11) print(str12) print(str13) print(str14) ================================================= Finding of Substrings ================ 1) In forward direction: ================= left to right ==> two inbuilt methods: 1) find() 2) index() Here: both methods can return: index value of the sub-string 1) find() ====== Syntax: str_object.find('sub-string') ==> when the sub-string is at multiple places, find() can return the first occurrence ==> when the sub-string is not the part of the string, find() can return '-1' ==> using find(), we can search about the sub-string from the given range. ==> when we defined the sub-string with more than one character, find() returns: the index of First character of the sub-string only. str_data = "Python Programming" # finding of single character res1 = str_data.find('P') res2 = str_data.find('b') print(res1) print(res2) # finding the sub-string from the given range res3 = str_data.find('m',1,10) res4 = str_data.find('m',5,15) print(res3) print(res4) # sub-string with more than one character res5 = str_data.find('pro') res6 = str_data.find('Pro') print(res5) print(res6) =============================== 2) index() ======= Syntax: str_data.index('sub-string') ==> when the sub-string is at multiple places, index() can return the first occurrence ==> when the sub-string is not the part of the string, index() can return 'value error' ==> using find(), we can search about the sub-string from the given range. ==> when we defined the sub-string with more than one character, index() returns: the index of First character of the sub-string only. str_data = "Python Programming" res1 = str_data.index('m') # res2 = str_data.index("b") res2 = str_data.index('m',5,15) res3 = str_data.index("Program") print(res1) print(res2) print(res3) find() Vs index() ============= find() can return '-1', when the sub-string is not present in the given string data. index() can return 'value error', when the sub-string is not present in the given string data. ====================================== 2) in reverse direction: ================= ==> two inbuilt methods: 1) rfind() 2) rindex() Syntax: str_data.rfind('sub-string') str_data.rindex('sub-string') str_data = "Object Oriented Programming Language" print(str_data.rfind('O',1,10)) print(str_data.rfind('m')) print(str_data.rfind('MN')) print(str_data.rindex('O',1,10)) print(str_data.rindex('m')) print(str_data.rindex('MN')) ======================================== # WAP IN PYTHON TO DISPLAY ALL POSITIONS OF THE SUB-STRING IN A GIVEN MAIN STRING. data = input("Enter a string:") subs = input("Enter a sub-string:") flag = False length = len(data) pos = -1 while True: pos = data.find(subs, pos + 1, length) if pos == -1: break print("The Character at:", pos) flag = True if not flag: print("The Given sub-string is not found in a main string.") ====================================================== Counting of number occurrences of the given sub-string in the main string ========================================================= count() ===== Syntax: str_data.count('sub-string') str_data.count('sub-string',start,stop) str_data = "object oriented programming language" print("The Total occurrence of \'o\' is = ",str_data.count('o')) print("The Total occurrence of \'r\' is = ",str_data.count('r')) print("The Total occurrence of \'o\' is = ",str_data.count('o',0,15)) ======================================= Assignment: ========== 1) WAP TO COUNT THE NUMBER OF OCCURRENCES OF ALL THE CHARACTERS OF THE GIVEN STRING. EX: "PYTHON PROGRAMMING" EXP_OUTPUT: =========== P ==> 2 Y ==> 1 T ==> 1