You need to use string slicing. For example:
>>> my_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# For odd
>>> my_str[::2]
'ACEGIKMOQSUWY'
# For even
>>> my_str[1::2]
'BDFHJLNPRTVXZ'
General syntax of string slicing is string[start:end:jump]
where:
start
: is the starting index for slicing the string. Empty value means start of the string i.e. index 0
end
: is the index till which you want to slice the string. Empty value means end of the string
jump
: is used to jump the elements from start
such that you'll get values in the order start
, start+jump
, start+2*jump
, so on till your string reaches end
. Empty value means 1