Search code examples
pythonindexingfindraw-input

using find in python


New to python, my assignment asks to ask user for input and then find and print the first letter of each word in the sentence

so far all I have is

phrase = raw_input("Please enter a sentence of 3 or 4 words: ")

^ That is all I have. So say the user enters the phrase "hey how are you" I am supposed to find and print the first letter of every word so it would print "hhay"

I know how to index if it is a string that the programmer types but not when a user inputs the data.


Solution

  • This does everything that Ming said in a single line. You can very well understand this code if you read his explanation.

    phrase = raw_input("Please enter a sentence of 3 or 4 words: ")    
    output = ''.join([x[0] for x in phrase.split()])    
    print output
    

    Update related to comment (Considers only first 3 words):

    output = ''.join([x[0] for x in phrase.split()])[:3]
    

    Ignoring the last word (Total number of words doesn't matter)

    output = ''.join([x[0] for x in phrase.split()])[:-1]