Write programs that read a line of input as a string and print every second letter of the string in Python?
So far I have written:
string=input('Enter String: ')
for char in range(0,len(string),2):
if i input a string:
it should print "
You need to keep it much simpler than this. If you enter a word and are looking to slice it at a specific point, use slicing.
qwerty it should print "qet"
So, you are looking to print every second letter:
>>> a = "querty" >>> a[::2] 'qet'
Slicing works like this:
[from start: from end: step]
So, in your case, you are looking to simply print every second, so you want to make use of your
step. So, simply slice leaving the
end empty, since you want to position yourself at the beginning of the string and then simply go every second. This is the reasoning behind using
Every second letter should start with index of 1 not 0. So, if your input is "qwerty", you output should be "wry".
Code below may be able to answer your question.
sentence = input("\nPlease enter a string : ") print("Every second letter of the string " + sentence + " is ", end="") for i in range(len(sentence)): if i % 2 == 1: print(sentence[i] + " ", end="")