0

Please enter a sentence: The quick brown fox jumps over the lazy dog.

Output: The brown jumps the dog

I've been doing some learning in strings in python, but no matter what I do, I can't seem to write a program that will remove the 2nd letter of every sentence.

word=(input ("enter setence"))

del word[::2]

print(word[char], 
end="")

Print("\n")

This was my closest attempt. At least I was able to write the sentence on the command prompt but was unable to get the required output.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • And what's your question? Or you think somebody will do your homework? – mechnicov Feb 24 '19 at 23:44
  • I did give this problem a try but I was unable to get the solution, I researched strings in python but I was still not able to get an answer. So I just needed some help from others on what I did wrong and learn from my mistake. – Eoin McLoughlin Feb 25 '19 at 23:05

4 Answers4

3
string = 'The quick brown fox jumps over the lazy dog.'
even_words = string.split(' ')[::2]

You split the original string using spaces, then you take every other word from it with the [::2] splice.

Merig
  • 1,751
  • 2
  • 13
  • 18
  • See Also: [Slice Notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Alec Feb 24 '19 at 22:48
0

Try something like :

" ".join(c for c in word.split(" ")[::2])

machine424
  • 155
  • 2
  • 9
0

Try this:

sentenceInput=(input ("Please enter sentence: "))

# Function for deleting every 2nd word
def wordDelete(sentence):

    # Splitting sentence into pieces by thinking they're seperated by space.
    # Comma and other signs are kept.
    sentenceList = sentence.split(" ")

    # Checking if sentence contains more than 1 word seperated and only then remove the word
    if len(sentenceList) > 1:
        sentenceList.remove(sentenceList[1])

    # If not raise the exception
    else:
        print("Entered sentence does not contain two words.")
        raise Exception

    # Re-joining sentence
    droppedSentence = ' '.join(sentenceList)

    # Returning the result
    return droppedSentence

wordDelete(sentenceInput)
Koray B
  • 26
  • 1
0

Try something like that.

sentence = input("enter sentence: ") words = sentence.split(' ') print(words[::2])

xyhlon
  • 95
  • 2
  • 9