How to remove the characters which have odd index values of a given string in python?

Can anyone help me with the source code in python which can be used to remove the characters which have odd index in a give string.

For example if the given string is 'python' then I am looking for the output 'pto'. My index is starting from 0.

1 Answers

You can use '%' to write related program. If mod of 2 is equal to 0 you can take that character and if it is not equal to 0, you can ignore that character.

def find_odd_vals(str):
  final_str = "" 
  for i in range(len(str)):
    if i % 2 == 0:
      final_str = final_str + str[i]
  return final_str


print(find_odd_vals('python'))
print(find_odd_vals('gaurav'))

The above code will print the output as:

'pto'

'gua'

Ask question now
Never leave your website again in search of code snippets by installing our chrome extension.