STRING OPERATIONS ================== REPLACING OF SUB-STRING WITH ANOTHER STRING: ============================================ 'PYTHON IS DIFFICULT PROGRAMMING LANGUAGE' REPLACE 'DIFFICULT' WITH 'EASY' ==> 'PYTHON IS EASY PROGRAMMING LANGUAGE' replace(): ========== can use to replace the old string with new string. Syntax: new-string-object = string-data.replace(old-substring, new-substring) Note: ===== if the specified sub-string is not in the given string, then: replace() cannot return any error. The original string is only considered to return as new string-object. # Replacing Operation str_data = "Python is Difficult programming language." print("The Original data before the replace operation is:") print(str_data) print(id(str_data)) res_data = str_data.replace('Difficult','easy') print("The Data after the replace operation is:") print(res_data) print(id(res_data)) res1_data = str_data.replace('difficult','easy') print(res1_data) print(id(res1_data)) =========================================== String to List Conversion: =========================== or Splitting of the string: ============================= What is list? ============= ==> List is a collection data ==> group of homogeneous (similar data items) or heterogeneous (different data items) elements. ==> which it can define with '[]' all the elements in a list must be placed within [] by the comma separation. Syntax: list-object-name = [item1,item2,item3,...] ==> List is an inbuilt datatype for the list datatype, an inbuilt class: list ==> List data is an ordered. # List Definition """ camel case: in an identifier, the first word is completely with lower case alphabets and from the second should start with capital letter and remaining (except first letter) should be in lower case. if an identifier with single word/single character ==> preferred with lower case only. """ listDataStructure = [] # empty list ld1 = [1,3,5,7,9] # homogeneous list, because this list is with only integers. ld2 = [1.3,2.4,121,True,12-24j,'abcd'] # heterogeneous list print(type(listDataStructure),type(ld1),type(ld2)) print(listDataStructure);print(ld1);print(ld2) =============================== split() ======= it can use to convert a string data into a list based on the given separator. Ex: 1) "python programming language" separator : space op ==> ['python', 'programming', 'language'] 2) "26-08-2024" separator : '-' op ==> ['26','08','2024'] Syntax: str-data.split('separator') Note: split() can always return a list as an output. # String to list conversion strData1 = "Python Programming Language" strData2 = "PythonpProgrammingpLangauge" date1 = "26-08-2024" date2 = "26/08/2024" date3 = "26.08.2024" listData1 = strData1.split() # in this case, separator is : space # listData2 = strData1.split('') listData3 = strData1.split(' ') listDat2 = strData2.split('p') dateList1 = date1.split('-') dateList2 = date2.split('/') dateList3 = date3.split('.') print(listData1);print(listData3) print(listDat2) print(dateList1);print(dateList2);print(dateList3) ============================================ Conversion of list data into string data: ========================================= or Joining of list elements into single unit (string): =================================================== join(): ======= can use to join an individual elements of a collection like list into string data. Syntax: 'separator'.join(collection-data) Ex: ['26','08','2024'] output: "26-08-2024" # Joining of List elements into string listData1 = ['26','08','2024'] listData2 = ['Python','Programming','Language'] strData1 = '-'.join(listData1) strData2 = '/'.join(listData1) strData3 = '.'.join(listData1) strData4 = ' '.join(listData2) strData5 = ''.join(listData2) print(strData1);print(strData2);print(strData3) print(strData4) print(strData5) ================================================= startswith(): ============ ==> it can use to check whether the given string can start with specified sub-string or not. ex: 'www.google.com' url has starts with: 'https://' ==> startswith() can always return a Boolean value. Syntax: str-data.startswith('sub-string') # stratswith() url = "https://www.google.com" url1 = "http://ravitechzone.com" print(url.startswith('https://')) print(url1.startswith(('https://'))) if url1.startswith('https://'): print("The Given Web application is most Secure.") else: print("The Given Web application is not secure.") ================================================ endswith(): =========== it can use to check whether the given string is end with the given specified sub-string or not. Syntax: str-data.endswith('sub-string') # endswith() mail = input("Enter a user mail id:") if mail.endswith('@gmail.com' or '@rediffmail.com' or '@yahoo.com'): print("The Entered mail id is valid.") else: print("The Entered mail id is invalid.") ================================================ Checking of characters present in a string: =========================================== isalnum(): ========= can use to check whether the given string with only alphabets and numbers or not. Syntax: str-data.isalnum() isdigit(): ========== can use to check whether the given string with only digits or not. Syntax: str-data.isdigit() isalpha(): ========== can use to check whether the given string with only alphabets or not. Syntax: str-data.isalpha() islower(): ========== can use to check whether the given string with only lower case alphabets or not. Syntax: str-data.islower() isupper(): ========== can use to check whether the given string with only upper case alphabets or not. Syntax: str-data.isupper() istitle(): ========== can use to check whether the given string in the title case format or not. Syntax: str-data.istitle() isspace(): ========= when the string with only spaces, isspace() can return: "True". Otherwise, it can return: "False" Syntax: str-data.isspace() s1 = "ravi" s2 = "ravi2019" s3 = "ravi@2019" s4 = "2019" s5 = "RAVI" s6 = "Python Programming" s7 = "python Programming" s8 = "Python programming" s9 = "" s10 = " " s11 = " a b " print(s1.isalnum());print(s2.isalnum());print(s3.isalnum());print(s4.isalnum()) print(s1.isdigit());print(s2.isdigit());print(s3.isdigit());print(s4.isdigit()) print(s1.isalpha());print(s2.isalpha());print(s3.isalpha());print(s4.isalpha()) print(s1.islower());print(s2.islower());print(s3.islower());print(s4.islower());print(s5.islower()) print(s1.isupper());print(s2.isupper());print(s3.isupper());print(s4.isupper());print(s5.isupper()) print(s6.istitle());print(s7.istitle());print(s8.istitle()) print(s9.isspace());print(s10.isspace());print(s11.isspace())