Implement a function to swap the odd and even-indexed characters in a given string.swap_odd_even_chars(String text): that takes a string as input and returns the odd and even-indexed characters swapped string as output.Example - 1 : Input:String text = “abcdefgh”swap_odd_even_chars(text) Output: 'badcfehg'
Question
Implement a function to swap the odd and even-indexed characters in a given string.swap_odd_even_chars(String text): that takes a string as input and returns the odd and even-indexed characters swapped string as output.Example - 1 : Input:String text = “abcdefgh”swap_odd_even_chars(text) Output: 'badcfehg'
Solution
Sure, here is a Python function that swaps the odd and even-indexed characters in a given string:
def swap_odd_even_chars(text):
text = list(text)
for i in range(0, len(text)-1, 2):
text[i], text[i+1] = text[i+1], text[i]
return ''.join(text)
This function works by converting the input string to a list of characters, then iterating over the list in steps of 2. For each pair of characters, it swaps them by using a simple tuple assignment. Finally, it joins the list back into a string and returns it.
Here's how you can use this function with your example:
text = "abcdefgh"
print(swap_odd_even_chars(text)) # Output: 'badcfehg'
This will print 'badcfehg', which is the result of swapping the odd and even-indexed characters in the string 'abcdefgh'.
Similar Questions
Write a Python program to remove the characters which have odd index values of a given string.Note: Index starts from 0Input format :Input consists of a string.Output format :Output prints the string after removing the characters which have odd index values.Sample test cases :Input 1 :placementOutput 1 :paeetInput 2 :examlyOutput 2 :eal
bi, an aspiring programmer, is intrigued by character manipulation. She seeks your assistance to write a program that takes two characters as inputs and implements a function called swapCase(), swaps their positions along with case conversion, and prints the modified characters.Input format :The input consists of two space-separated characters.Output format :The output displays the two space-separated characters, representing the modified values after swapping and converting cases.Refer to the sample output for formatting specifications.Code constraints :Input characters should be 'a' to 'z' or 'A' to 'Z'.Sample test cases :Input 1 :a ZOutput 1 :z AInput 2 :d kOutput 2 :K DInput 3 :K sOutput 3 :S kInput 4 :M TOutput 4 :t mInput 5 :Z aOutput 5 :A z
Word Reversal in a StringDesign a Python program to accept a string from user.The string can be composed of words separated by a spaces. Implement the code logic to modify and return the given string ‘str’ such that its every word is inverted but appears in the same order as the original string.Constraints:NAExample:Sample Input:Python StringsSample Output:nohtyP sgnirtS
Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.
Develop an assembly language program that swaps the values of two strings, which can be of any length.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.