Search code examples
functiontextreturncapitalize

Extract first character of each word from a text and capitalize it with a function and return method


def initials(text): result=bla bla bla return result

main

text=input("Please enter your text") initials(text)


Solution

  • So here is the list of tasks we will do:

    Split the sentence into a list of words - step 1
    Get the first letter from each word in upper case - step 2
    Join them in this format: {letter}. {another-letter} - step 3
    Return the value and print it - step 4

    I've marked each step in the code comments
    So lets go:

    my_word = "stack overflow" # < Change this to any word you'd like
    def get_initials(input_word):
      result = input_word.split() # step - 1
      final_result = ""
      for word in result: # loop through our word list
        first_letter = word[0].upper() # step - 2 
        final_result += first_letter + '. ' # step - 3, join
        '''
        So basically get the first letter with word[0]
        and add it to the final_result variable using +=
        and also add an additional ". "(a dot with a space)
        each time, as per requirements
        '''
    
      return final_result # step - 4, return
    
    print ( get_initials(my_word) ) # and finally step - 4, print
    

    Hope this helped :)