Write A Python Program To Remove The Characters Which Have Odd Index Values Of A Given String

Given a string as an input to the Python program your task is to remove all the characters that are the letters present in the string which are odd index values and retain only even index values/  letters.

Let's create a new string and copy the original string by removing the odd indexed values and retain the even  index values.

 

Write A Python Program To Remove The Characters Which Have Odd Index Values Of A Given String

 

 


Write A Python Program To Remove The Characters Which Have Odd Index Values Of A Given String

Let's take a variable named as text and and ask the user to enter  a string using the input function call.  Once the user enters a string, take another variable named as new and dispersion and initialize it to an Empty string.

text = input("Enter a String : ")
new_string = ""




Once we get the input string to the program, let's enumerate using the following and get the index and the individual letters present in the string.  using the if statement try to divide the index by 2 if the index is completely divisible by 2 then at the  the letter to the new_string  variable.  this will create a new string with even set of values by eliminating the odd set of values

for index, letter in enumerate(text):
   if index % 2 == 0:
       new_string = new_string + letter




Once the for loop is completed using print statement to print a user-friendly message that is string after removing the odd index values are and print the new_string

print("String after removing Odd Indexed Values ...")
print(new_string)




Complete Python working
===============================
text = input("Enter a String : ")
new_string = ""

for index, letter in enumerate(text):
   if index % 2 == 0:
       new_string = new_string + letter

print("String after removing Odd Indexed Values ...")
print(new_string)




OUTPUT
===================
Enter a String : how are you ?
String after removing Odd Indexed Values ...
hwaeyu?




OUTPUT
================
Enter a String : Write A Python Program To Remove The Characters Which Have Odd Index Values Of A Given String
String after removing Odd Indexed Values ...
WieAPto rga oRmv h hrcesWihHv d ne ausO  ie tig




Conclusion
===============
Execute the above program by yourself  by providing input as a random string to the Python program,  not down the results.

Comment it down below if you have any suggestions regarding the above program
 


Post a Comment

0 Comments