String Manipulations ==================== What is string? How to define Multi-line string? Accessing of characters of the string 1) Indexing 2) Slicing 3) Looping/Iteration 4) With methods String ==> Group Characters that each character in a string ==> Substring String ==> Group of substrings Accessing of characters/substrings of the string ================================================ 1) find() ========= -> return the index of the first occurrence of the given sub-string and also returns '-1' if the given substring is not found in the given string. Ex: slicing s = "Object Oriented Programming Language" s[28:] ==> Language Syntax: string-name.find('substring') s = "Object Oriented Programming Language" i1 = s.find('L') i2 = s.find('e') i3 = s.find('Programming') i4 = s.find("python") print("The Index of the Sepcified Substring = ",i1) print("The Index of the Sepcified Substring = ",i2) print("The Index of the Sepcified Substring = ",i3) print("The Index of the Sepcified Substring = ",i4) Note: ==== find() can always search about the given substring in the given string from left to right direction (Forward Access). ====================================================== rfind() ======= Syntax: string-name.find('substring') -> rfind() can accept the substring and return the first occurrence of the given substring in the string if it will find. otherwise rfind() returns '-1'. -> rfind() can find the given substring in reverse access (right to left). s = "Object Oriented Programming Language" # By default, the PVM consider the positive indexing only i1 = s.find('L') i2 = s.rfind('L') i3 = s.find('O') i4 = s.rfind('O') print("The Index of 'L' = ",i1) print("The Index of 'L' = ",i2) print("The Index of 'O' = ",i3) print("The Index of 'O' = ",i4) ============================================================== 2) index() ========== Syntax: string-name.index('substring') -> index() is same as find() can return the first occurrence of the given substring if identified otherwise, it will return "value error". s = "Object Oriented Programming Language" print(s.index('L')) print(s.index('e')) print(s.index('Programming')) print(s.index("python")) Note: ===== like find(), index() also search about the given substring in forward access only. rindex() ======== Syntax: string-name.rindex('substring') ===================================================== Math Operations on Strings ========================== two math operations: 1) String concatenation 2) String Repetition 1) String concatenation ======================= -> joining of two or more strings into one called as "String concatenation" -> Symbol: '+' Syntax: str1 + str2 + str3 + .... str1str2 + str3 ==> str1str2str3 Note: ===== In Python, the string concatenation is strictly considered on only strings. 2) String Repetition ==================== -> can use to define with '*' -> when we want to repeat the same string for multiple times, we can use "string repetition" operation. Syntax: string-object * n here: n ==> number of times Is string immutable? ==================== Ans: Yes -> Immutability and mutability are two terms we can consider to categorize the datatypes. -> Immutable datatypes: we cannot modify after the definition Ex: all primitive datatypes -> If we can perform the re-assignment, we can get new memory address location # string COncatenation str1 = "Object " str2 = "Oriented " str3 = "Programming " str4 = "Language" s = str1 + str2 + str3 + str4 print("The Concatenated String is = ",s) s1 = str1 * 4 print("The Repeated String = ",s1) print("Before the modification:") print(str1) print(id(str1)) str1 = str1 + str2 # re-definition print("After the modification:") print(str1) print(id(str1)) # str1[0] = 'o' Type Error print(str1) -> Mutable datatypes: are allowed for the modification after the definition without changing the address location. Ex: List, Sets, Dictionary etc. =============================================================== String Comparison: ================= Python-2: ======== cmp(), we have used for the comparison on collections. but, in python-3 cmp() got deprecated. Syntax: cmp(s1,s2) +eve value ==> s1 > s2 0 ==> s1 == s2 -eve value ==> s1 < s2 A to Z ==> 65 to 90 a to z ==> 97 to 122 1 to 9 ==> 48 to 57 ASCII ==> 256 values for 256 characters only. Unicode ==> 65536 values for 65536 characters. => In python-3, the string comparison is allowed with relational operators only. s1 = "python" s2 = "Python" s3 = "PYTHON" print(s1 == s2) print(s2 == s3) print(s1 > s2) print(s1 > s3) print(s2 > s3) ===================================================== How to remove leading spaces from the string: ============================================= Ex: " Python" "Python " " Python " "Python Programming" ==> not case of leading spaces strip() lstrip() rstrip() s1 = " Ashok IT" s2 = "Ashok IT " s3 = " Ashok IT " # strip() can remove leading spaces from left side and right side of the string # s1 = s1.strip() # s2 = s2.strip() # s3 = s3.strip() # lstrip() can remove lefi-side leading spaces only. # s1 = s1.lstrip() # s2 = s2.lstrip() # s3 = s3.lstrip() # rstrip() can remove right side leading spaces only s1 = s1.rstrip() s2 = s2.rstrip() s3 = s3.rstrip() print(s1) print(s2) print(s3) =============================================== Assignment: =========== 1) WAP TO DISPLAY ALL POSITIONS OF THE SUBSTRING IN A GIVEN MAIN STRING. Hint: ==== String = "Object Oriented" substring = "O" Position of 'O' ==> 0 Position of 'O' ==> 7