I need help writing a program that will read a string from the user and print the string with every other word removed.
Ex.
Input sentence:
Hello everybody
Eloeeyoy

Recommended Answers

Your example answer is wrong.

Jump to Post

That gives wrong output james.lu.75491856.
There is no need to split and loop over words.

input_string = raw_input("Input")
print input_string[::2]
Jump to Post

All 5 Replies

Your example answer is wrong.

here is another example
Input string: Hi! Puctuation counts as a character.
H!Pcuto onsa hrce.

>>> 'Hi! Puctuation counts as a character.'[::2]
'H!Pcuto onsa  hrce.'

Python slice notation.

'Hi! Puctuation counts as a character.'[start:stop:step]

Start and stop(deafault :) start(H) and stop(.)
If step is positve we are moving forwards starting at(H)(if the value of 'step' is ommited, it defaults to +1)

input_string = raw_input("Input")
words = input_string.split()
for i in words[::2]:
    print i

words is a list, assuemes words are seperated by a space.

That gives wrong output james.lu.75491856.
There is no need to split and loop over words.

input_string = raw_input("Input")
print input_string[::2]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.