String Practice: ================ # Practice with strings # WAP IN PYTHON TO ACCEPT A STRING AND PRINT THE STRING REVERSE. def reverseString(string): # reverseString = string[::-1] length = len(string) index = length - 1 reverseString = "" while index >= 0: reverseString = reverseString + string[index] index -= 1 return reverseString strData = input("Enter a string:") output = reverseString(strData) print("The string after the reverse = ",output) ============================================= # WAP TO TAKE A STRING AS AN INPUT AND PRINT THE STRING WITH REVERSE OF WORDS. """ IP = "PYTHON PROGRAMMING LANGUAGE" OP = "LANGUAGE PROGRAMMING PYTHON 1) take an input of string 2) string ==> list, split() 3) reverse of the list data. 4) list ==> string, join() 5) Print that reverse string """ s = input("Enter a string data:") ld = s.split() # converting a string into list rev_list = ld[::-1] # list content can be reversed result = ' '.join(rev_list) print("The STring with reverse ordered words = ",result) ======================================================== # WAP IN PYTHON TO ACCEPT A STRING AND PRINT THE STRING WITH REVERSE OF THE INTERNAL CONTENT OF EACH WORD. """ IP : PYTHON PROGRAMMING LANGUAGE ld = [python, programming, language] OP : NOHTYP GNIMMARGORP EGAUGNAL 1) TAKE THE STRING ASN AN INPUT 2) STRING ==> LIST, split() 3) APPLY LOGIC FOR REVERSE OF EACH WORD/ELEMENT ON THE LIST 4) JOIN THE LIST TO STRING 5) PRINT THAT STRING """ s = input("Enter a string:") ld = s.split() # string to the list # print("The List after the split operation is = ",ld) res_list = [] index = 0 while index < len(ld): res_list.append(ld[index][::-1]) index += 1 # print(res_list) res_string = ' '.join(res_list) print(res_string) ====================================== # WAP TO PRINT CHARACTERS AT EVEN POSITIONS AND AT ODD POSITIONS. s = input("Enter the string:") print("The Characters at Even Places are:",s[0::2]) print("The Characters at Odd Places are:",s[1::2]) ================================ s = input("Enter the string:") # print("The Characters at Even Places are:",s[0::2]) # print("The Characters at Odd Places are:",s[1::2]) index = 0 s_even = "" s_odd = "" while index < len(s): if index % 2 == 0: s_even = s_even + s[index] else: s_odd = s_odd + s[index] index += 1 print("The Characters at Even place = ",s_even) print("The Characters at Odd place = ",s_odd) ======================================= # WAP TO MERGE CHARACTERS OF 2 STRINGS INTO A SINGLE STRING BY TAKING CHARACTERS ALTERNATIVELY. """ S1 = "PYTHON" S2 = "PROGRAM" OP = PPYRTOHGORNAM" 1. define two strings with same length 2. traversing on string for merging the content """ s1 = input("Enter a string1:") s2 = input("Enter a string2:") result = "" i,j = 0,0 while i < len(s1) or j < len(s2): if i < len(s1): result = result + s1[i] i += 1 if j < len(s2): result = result + s2[j] j += 1 print("The String after the merge = ",result)