String Manipulations: ===================== Java Strings Vs Python Strings: =============================== In java: ======= equals(): ======== s1.equals(s2) ==> compare the string's internal content == === s1 == s2 ==> to compare the address of two strings contains() ========== s1.contains("sub-string") In python: ========= for string internal content validation ==> "== operator" s1 == s2 for validating the addresses of two stings: identity operators (is/is not) s1 is s2 for membership check: Membership operators (in/not in) 'sub' in s1 # Identity operators and Membership operators s1 = "python" s2 = "python" s3 = "Python" print(id(s1)) print(id(s2)) print(id(s3)) print(s1 is s2) print(s1 is s3) print('P' in s3) print(s1 == s2) ================================================= -> Python strings allows the duplicated characters. "level" count(): ======== -> can use to count the number of occurrences of the specified character/sub-string within the given main string. Syntax: string-name.count('sub-string') -> count() can also return '0' when the given sub-string is not present in the main string. -> count() can also use to find the number of occurrences of the sub-string from the specified range of the main string. Syntax: string-name.count('substring', start, stop) s1 = "level" s2 = "Python is Object Oriented Programming Language" print(s1) print("The Number of occurrences for letter 'l' is = ",s1.count('l')) print("The Number of occurrences for letter 'e' is = ",s1.count('e')) print("The Number of occurrences for letter 'v' is = ",s1.count('v')) print("The Number of occurrences for letter 'a' is = ",s1.count('a')) print("The Number of occurrences for letter 'e' is = ",s2.count('e',10,25)) print("The Number of occurrences for letter 'e' is = ",s2.count('e',s2.find('O'),s2.find('d'))) a = s2.find('O') b = s2.find('d') print("The Number of occurrences for letter 'e' is = ",s2.count('e',a,b)) ============================================================= replace(): ========== -> can use to replace the old-substring with new-substring. Syntax: string-name.replace('old-substring', 'new-substring') s = "Python is Difficult programming langauge" print(s) s = s.replace('Difficult','Easy') print(s) ========================================= Q: Is it possible to convert a string into List data? ====================================================== Ans: Yes split(): ======== -> we can convert the main string into list data. Syntax: list-data-name = string-name.split('separator') # string to list conversion s1 = "Python is Easy" s2 = "ravi.vrao.infs@gmail.com" s3 = "29-01-2025" s4 = "s9/01/2025" s5 = "123aa345aa567aa789" l1 = s1.split() l2 = s1.split(' ') l3 = s2.split('.') l4 = s3.split('-') l5 = s4.split('/') l6 = s5.split('aa') print(l1) print(l2) print(l3) print(l4) print(l5) print(l6) Q: Is it possible to convert a collection of strings into main string? ====================================================================== Ans: Yes ('29','01','2025') ['Ashok IT','Welcomes', 'You'] join() ====== => based on the specified separator, the collection of strings can join into one main string. Syntax: 'separator'.join(collection of strings) c1 = ('29','01','2025') c2 = ['Ashok',"IT","Welcomes","You"] s1 = ''.join(c1) s2 = ' '.join(c1) s3 = '-'.join(c1) s4 = ' '.join(c2) print(s1) print(s2) print(s3) print(s4) ============================================== case change: ============ # case change s1 = "python".upper() s2 = "PYTHON".lower() s3 = "PyThOn pRoGrAmS".swapcase() s4 = "python programming langauge".title() # titlce case s5 = "python programming language".capitalize() print(s1) print(s2) print(s3) print(s4) print(s5) ================================================= String validation: ================== startswith() endswith() isdigit() isalnum() isalpha() islower() isupper() istitle() # String validations # mail id validation mail = "ravivraoinfs@gmail.com" url = "https://www.ashokit.in" username = "ArjunKumar" # if mail.endswith("@gmail.com"): # print("The enetered mail id is valid.") # else: # print("The entered mail id is invalid.") if url.startswith("https://"): print("The Given URL is valid.") else: print("The given URL is invalid.") print(username.isalpha())