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
Question
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
Solution
Sure, here is a Python program that removes the characters which have odd index values of a given string.
def remove_odd_index_chars(str):
result = ""
for i in range(len(str)):
if i % 2 == 0:
result = result + str[i]
return result
# Test the function with the sample test cases
print(remove_odd_index_chars("placement")) # Output: paeet
print(remove_odd_index_chars("examly")) # Output: eal
In this program, we define a function remove_odd_index_chars that takes a string str as input. We initialize an empty string result. Then we iterate over the length of the input string. If the index i is even (i.e., i % 2 == 0), we add the character at that index to result. Finally, we return result.
We then test this function with the sample test cases "placement" and "examly".
Similar Questions
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'
list_of_strings = input().split(',')# 🚨 Do not change the code above# TODO: Use list comprehension to convert the strings to integers 👇:# TODO: Use list comprehension to filter out the odd numbers# and store the even numbers in a list called "result"
Write a Python program that prompts the user to input a list of integers separated by commas. Your task is to modify the list so that all elements become odd by incrementing each even element by 1, and then print the modified list.Input format:The input consists of a list of integers separated by commas.Output format:Output the modified list where all elements are odd.Sample Test CasesTest Case 1:Expected Output:1,3,5,7[1,·3,·5,·7]Test Case 2:Expected Output:2,1,5,8[3,·1,·5,·9]
Write a program to print the odd values in the given rangeInput FormatTwo space separated integers - 'start' and 'end'Output FormatDisplay all the odd numbers between 'start' and 'end'Sample Input10 20Sample Output11 13 15 17 19
Digit SeparationA Competition is scheduled in Buddy4Study Primary School so that the KG students can get some appreciation in understanding odd and even numbers. The task is to identify the even digits which are both preceded and followed by an odd digit. As the number of student registrations is high, your help in this regard is highly expected. Develop a python program for generating the output as shown in sample input and output. The first and last digits cannot be included in output, as they donot have preceding digit and following digit, respectively. If no such digit is found, print "NIL".Sample Input:56474182729Sample Output: (Display output digits, each in a separate line)42
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.