STRING OPERATIONS ================== REMOVAL OF SPACES FROM THE GIVEN STRING ========================================== """ strings ==> collection of characters which enclosed with single quotes or double quotes. characters ==> alphabets (upper case/lower case), digits (0 to 9) and special characters includes space Note: ==== when the string contains the space in between the string ex: "python programming" ==> the removal of the spa ce is no possible. when the string contains the spaces in left (at the beginning) or in right (at the end) ex: " Python", "Python ", " python " ==> spaces are considered to remove. ==> to remove the spaces from the string: three inbuilt methods: 1) strip() 2) lstrip() 3) rstrip() 1) strip() ========== Syntax: string_object.strip() ==> strip(), can allow to remove the spaces of the string in both left side spaces and in right spaces also. 2) lstrip() ========== Syntax: string_object.lstrip() ==> lstrip() can remove the spaces from the left side of the string only. 3) rstrip() ========== Syntax: string_object.rstrip() ==> rstrip() can remove the spaces from right part of the string only. """ s1 = "Python Programming" s2 = " Python" s3 = "Python " s4 = " Python " print(s1) print(s2) print(s3) print(s4) print() r1 = s1.strip() r2 = s2.strip() r3 = s3.strip() r4 = s4.strip() print(r1) print(r2) print(r3) print(r4) print() r11 = s2.lstrip() r12 = s3.lstrip() r13 = s4.lstrip() print(r11) print(r12) print(r13) print() r21 = s2.rstrip() r22 = s3.rstrip() r23 = s4.rstrip() print(r21) print(r22) print(r23) ================================== FINDING OF SUB-STRINGS ====================== ==> POSSIBLE IN TWO APPROACHES: 1) FORWARD DIRECTION (LEFT TO RIGHT) 2) REVERSE DIRECTION (RIGHT TO LEFT) 1) FORWARD DIRECTION (LEFT TO RIGHT) ==================================== find() ==== Syntax: string_object.find('Sub-string') str1 = "High Level Programming Language" # when the given sub-string is at multiple places # the first index of the given sub-string can return (The first Occurrence of the specified sub-string) print(str1.find('i')) print(str1.find('a')) # when the sub-string is not present in the given string # find() can return '-1' print(str1.find('z')) # if the sub-string with more than one character print(str1.find('Level')) print(str1.find("level")) # find() can use to find the sub-string within the specified range also # str_data.find(sub-string,start, end) print(str1.find('L',1,15)) ======================================================= index() ====== Syntax: str_data.index('sub-string') s1 = "Python Programming Language" print(s1.index('P')) # index() can always return the first occurrence of the given sub-string # print(s1.index('b')) # index() can return "value error", when the sub-string is not present in the given string. print(s1.index('a',5,20)) =================================== find() Vs index() ============ find() can return: '-1' when the sub-string is not present in the given string index() can return "value error" when the sub-string is not present in the given string. ======================================================= 2) REVERSE DIRECTION (RIGHT TO LEFT) =================================== rfind() rindex() s1 = "general purpose programming language" print(s1.rfind('p')) print(s1.rfind('P')) print(s1.rfind('p',5,15)) print() print(s1.rindex('p')) # print(s1.rindex('P')) print(s1.rindex('p',5,15)) =============================================== COUNTING OF NUMBER OF OCCURRENCES OF THE GIVEN SUB-STRING IN THE GIVEN STRING =============================================================================== "ababcd" "a" ==> 2 "b" ==> 2 count() ====== Syntax: str_data.count('sub-string') s1 = ("Python Programming Language") # if the sub-string at multiple place print(s1.count('g')) print(s1.count('m')) # if the sub-string is not present # return: '0' print(s1.count('p')) print(s1.count('r',5,15)) ========================================== Assignment: ========== 1) WAP IN PYTHON TO DISPLAY ALL POSITIONS OF SUB-STRING IN A GIVEN STRING. MAIN STRING = "PYTHON PROGRAMMING LANGUAGE" SUB STRING = 'P' EXP_OUTPUT: ============ THE CHARACTER 'P' IS AT INDEX : 0 THE CHARACTER 'P' IS AT INDEX : 7