STRING OPERATIONS: ================== MATH OPERATIONS ON STRING DATA: =============================== 1) STRING CONCATENATION ======================== ==> JOINING OF TWO OR MORE STRINGS INTO ONE STRING IS CALLED AS STRING CONCATENATION. EX: "PYTHON" "PROGRAMMING" CONCATENATION RESULT ==> "PYTHONPROGRAMMING" ==> SYMBOL: '+' Syntax: str-data1 + str_data2 Rule: ===== String concatenation in python is always with string data. # STRING CONCATENATION str1 = "python" str2 = "programming" a = 9779 print("The String Concatenation Result = ") print(str1 + str2) print(str2 + str1) # print(str1+str2+a) 2) STRING REPETITION ===================== Repeating the content of the string data for number of times is called as "string repetition" ==> symbol: '*' Syntax: str-object * n str1 = "python" print("The Result of String Repetition is = ") print(str1 * 7) Mutability Vs Immutability: =========================== Immutability: ============= integer, float, complex Boolean String ====> Immutable Datatypes Once the data definition is completed, we cannot modify that definition. # Immutability a = 1212 b = 123.123 c = 123-234j d = True e = 'abcde' print("The Addresses of the given data are:") print(id(a),id(b),id(c),id(d),id(e)) # define the change a = 1234 b = 123.987 c = 123+234j d = False e = 'Python' print("The Addresses of the given data are:") print(id(a),id(b),id(c),id(d),id(e)) Mutability: =========== After the definition, the data can consider to modify within the same address location. Ex: List, Set, Dictionary etc. Is String Immutable? ==================== str1 = "python" str2 = "programming" print(id(str1)) str1 = str1 + str2 print(str1) print(id(str1)) str1 = str1.upper() print(str1) print(id(str1)) =================================================== Membership Check on String data: ================================ str1 = "Python Programming" print('Pro' in str1) print('program' in str1) print('Pro' not in str1) print('program' not in str1) ============================================== String Comparison: ================== Python2: ======= we had a method: cmp() ==== Syntax: cmp(o1,o2) return: 1 ==> o1 > o2 -1 ==> o1 < o2 0 ==> both objects are equal/same s1 = "Python" s2 = "python" print(cmp(s1,s2)) print(cmp(99,79)) # Unicode : # a to z ==> 97 to 122 # A to Z ==> 65 to 90 # 0 to 9 ==> 48 to 56 Note: ==== cmp() got deprecated from Python-3. for the string comparison ==> we can use "relational operators". s1 > s2 ==> True/False # String Comparison s1 = "python" s2 = "python" s3 = "pyThon" print(s1 == s2) print(s1 == s3) print(s1 > s2) print(s1 < s2) print(s1 > s3) print(s1 < s3) ========================================== Removing of spaces from the string ================================== 1) strip() ========== ==> An inbuilt method, which we can use to remove the spaces from left and right side of the string. " Python" "Python " " Python " strip() ==> "Python", "Python", "Python" Syntax: str-object.strip() # Removing of spaces from the string str1 = "Python " str2 = " Python" str3 = " Python " print(str1) print(str2) print(str3) print(str1.strip()) print(str2.strip()) print(str3.strip()) 2) rstrip(): ============ an inbuilt method which we can use to remove the spaces from the right side of the given string Syntax: str-object.rstrip() # Removing of spaces from the string str1 = "Python " str2 = " Python" str3 = " Python " print(str1) print(str2) print(str3) print(str1.rstrip()) print(str2.rstrip()) print(str3.rstrip()) 3) lstrip() =========== is an inbuilt method which we can use to remove the spaces from left side of the string. Syntax: str-object.lstrip() # Removing of spaces from the string str1 = "Python " str2 = " Python" str3 = " Python " print(str1) print(str2) print(str3) print(str1.lstrip()) print(str2.lstrip()) print(str3.lstrip()) Note: ==== Spaces between the string cannot be removed. "Python Programming".strip() ==> "Python Programming" ===================================================== Counting of Substrings from the given string: ============================================= To count the number of occurrences of the given sub-string in a given string, we can use count() method. Syntax: str-object.count('sub-string') return: '0', when the sub-string is not in the given string. return: number of occurrences, when the sub-string is in the given string. # Sub-string count st1 = "Python Programming" count1 = st1.count('P') count2 = st1.count('p') count3 = st1.count('m') count4 = st1.count('mm') print(count1) print(count2) print(count3) print(count4) =================================================== Finding of sub-strings ====================== Fining of sub-strings from left to right of the string is considered as "Forward finding" Finding of sub-strings from right to left of the string is considered as "Backward finding". Forward Finding: ================ find() ======= Syntax: str-object.find('sub-string') # find() Vs index() str1 = "Python Programming" # if the sub-string is at one place print(str1.find('h')) # if the sub-string is at multiple places, find() can return the first index value (occurrence) print(str1.find('n')) # if the sub-string is not in the given string, find() can return "-1" print(str1.find('-')) index() ======= Syntax: str-object.index() # find() Vs index() str1 = "Python Programming" # if the sub-string is at one place print(str1.index('h')) # if the sub-string is at multiple places, index() can return the first index value (occurrence) print(str1.index('n')) # if the sub-string is not in the given string, index() can return "Value Error" print(str1.index('-')) ==================================================== Reverse Finding: ================ rfind() ======= Syntax: str-object.rfind() rindex() ======== Syntax: str-object.rindex()