String Handling: ================ Finding of the Substring: ========================== index(): ======== Syntax: string-var.index(sub-string) # index() s = "Python Programming Language" print(s.index('g')) # first occurrence # print(s.index('G')) # Value Error # find() print(s.find('g')) # first occurrence print(s.find('G')) # -1 ============================================== # WAP IN PYTHON TO DISPLAY ALL THE POSITIONS OF SUBSTRING IN THE GIVEN STRING. s = input("Enter the string:") # main string sub = input("Enter the sub-string:") # sub-string position = -1 n = len(s) flag = False # infinite loop while True: position = s.find(sub,position+1,n) if position == -1: break print("The Sub-string is found at:",position) flag = True if flag == False: print("The Sub-string not found in anywhere of the string.") ============================================================================ WAP TO FIND THE LENGTH OF THE STRING OR NUMBER OF CHARACTERS OF STRING WITHOUT BUILT-IN METHOD. # data = input("Enter the string:") data = eval(input("Enter a string:")) # "Python" count = 0 for i in data: count += 1 print("The Total Number of characters = ",count) ============================================================ ==> if the sub-string is at multiple places/positions of the string, then: the sub-string ==> duplicated. Counting the substring in the string: ====================================== count(): ======== Syntax: str-object.count(sub-string) or str-object.count(sub, start, end) # count() # method-1 s = "Python Programming Language" # if the sub-string is not in the given string print(s.count('Z')) # if the sub-string is at multiple places print(s.count('g')) # method-2 print(s.count('r',0,10)) ============================================================= String Replace Operation: ========================== to replace one string with another string replace(): ========== Syntax: string-object.replace("old-string", "new-string") s = 'Python is Difficult Language' print("The String before the replace operation = ") print(s) result = s.replace("Difficult","Easy") print("The String after the replace operation = ") print(result) ===================================================== Convert a string into group of words (list) ====================================================== s1 = "Python is High Level Language" op = ["Python", "is", "High", "Level", "Language"] split(): ======= Syntax: string-object.split(separator) # split(): s = "Python is High Level Programming Language" s1 = "Python" s2 = "25-11-2024" s3 = "ObjectaaaOrientedaaaProgrammingaaaLanguage" print(s) # op1 = s.split(' ') op1 = s.split() op2 = s1.split() op3 = s2.split('-') op4 = s3.split('aaa') print(op1) print(op2) print(op3) print(op4) ==================================================== Joining of Strings : ==================== join(): ======= Syntax: 'separator'.join(list of strings) d = ["25","11","2024"] o1 = ''.join(d) o2 = ' '.join(d) o3 = '-'.join(d) print(o1) print(o2) print(o3) ======================================= Case Changing: ============== s = "Ashok IT" # lower() ==> string ==> lower case # upper() ==> string ===> upper case # swapcase() ==> string: lower ==> upper & upper ==> lower # title() ==> each words first character ==> capital and remaining ==>lower # capitalize() ==> in the entire string first character ==> capital, remaining ==> lower print("The String in Lower case = ",s.lower()) print("The String in Upper Case = ",s.upper()) print("The String in Swap case = ",s.swapcase()) print("The String in Title case = ",s.title()) print("The String Capitalize case = ",s.capitalize()) ============================================================ String Validations: =================== # String Validations ==> startswith() ====================================== startswith(): =========== True ==> the given string starts with the given sub-string. False ==> otherwise Syntax: str-obj.startswith('sub-string') url = input("Enter a valid URL:") if (url.startswith("https://")): print("The Given URL is secured.") else: print("The Given URL is not secured.") ================================================= # endswith() mail = input("Enter a correct mail id:") if (mail.endswith("@gmail.com") or mail.endswith("@yahoo.com")): print("The Entered email is valid mail id.") else: print("The entered email is invalid mail id.") ================================================= password = "Ravi@12345" print(password.isalnum()) # isalnum() ==> True(when if the string with only alphabets and numerals. print(password.isalpha()) print(password.islower()) print(password.isupper()) print(password.istitle()) Assignment: =========== 1) WAP IN PYTHON TO FIND THE SECOND OCCURRENCE OF THE GIVEN SUB-STRING. 2) WAP IN PYTHON TO PRINT THE CHARACTERS OF THE STRING FROM EVEN POSITIONS ONLY. Ex: s = "Python" s[0] = 'P' s[2] = 't' s[4' = 'o' 3) WAP TO CREATE A PASSWORD WITH ALPHA NUMERICALS ONLY. THIS PASSWORD SHOULD CONTAINS ATLEAST ONE UPPER CASE AND ATLEAST ONE LOWER CASE AND ONE DIGIT. AND THE PASSWORD LENGTH SHOULD NOT EXCEEDED THE 12 CHARACTERS.