String Manipulations ==================== Accessing of characters of the string: ====================================== 1) Indexing 2) Slicing 3) Looping for loop: ========= Syntax: for iteration-variable in string: // loop body Ex: string = "abcd" for i in string: print(i) #a b string = "abcde" print(string) for i in string: print(i) print(i) ================================================ # WAP TO ACCEPT A STRING AS AN INPUT AND PRINT ALL CHARACTERS OF THE STRING ALONG WITH ITS POSITIVE AND NEGATIVE INDEXES USING FOR LOOP. string = input("Enter any string:") index = 0 for r in string: print("The Character at positive index",index,"and at negative index",(index - len(string)),"is = ",r) index = index + 1 ================================================ Math operations on Strings: =========================== 1) String Concatenation ======================= -> Joining of two or more strings into one main string is called as "String concatenation". -> String concatenation can be used to represent with: '+' Ex: "str1" + "str2" + "str3" "str1str2" + "str3" output = "str1str2str3" str1 = "python " str2 = "is " str3 = "one of the " str4 = "scripting " str5 = "language" s = str1 + str2 + str3 + str4 + str5 print("The concatenated string = ",s) ==================================================== # WAP TO CHECK WHETHER THE GIVEN STRING IS PALINDROME OR NOT. """ if a string is palindrome: reverse of string == string ==> palindrome otherwise: not palindrome """ string = input("Enter some string:") reverseString = "" # empty string for i in string: reverseString = i + reverseString if reverseString == string: print("The given string is palindrome.") else: print("The given string is not palindrome.") ============================================================ Note: ===== Strings are immutable. Once you can define, there is no modification allowed to that string variable. str1 = "Python " str2 = "is easy" print("Before Modification:") print(str1) print(str2) print("str1 address = ",id(str1)) print("str2 address = ",id(str2)) str1 = str1 + str2 # re-assignment print("After the modification:") print(str1) print(str2) print("str1 address = ",id(str1)) print("str2 address = ",id(str2)) 2) String repetition ==================== when you want to repeat the string content for several number of times, we can use "string repetition". Syntax: string-name * n here: n ==> number of times str1 = "Python " result = str1 * 5 print(result) ====================================================== String Slicing: =============== -> when we want to acquire the piece or part of the string, we can use "slicing". -> slicing can be used to define with slice operator [] Syntax: string-name[start-index : stop-index : step-index] s = "Python is Easy Language" print(s[0:6:1]) # id the slicing need to start with first character of teh given string, then: start-index (0) is optional to specify in the slicing syntax. print(s[:6:1]) # if the slicing is needed with the index difference of '1', then: step-index also optional here. print(s[:6:]) print(s[:6]) print(s[:15:3]) print(s[15:23]) #suppose, the slicing is required till the last characcter of the string, then: stop-index is optional in the syntax. print(s[15:]) # if no start index and also no stop-index but step-index is added print(s[::1]) print(s[::]) # step-index as negative print(s[::-1]) # reverse of the string # start as negative # stop as negative print(s[-10:-1]) # start < stop print(s[10:1:-1]) print(s[-1:-10:-1])