-2

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

main

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

1 Answers1

0

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 :)

ICanKindOfCode
  • 1,000
  • 1
  • 11
  • 32
  • Thank you! Sorry for not being cleared. It's my first post.. Yes, I use python. the task is tο to construct a function, initials (text), which accepts text as input and returns the text with the original first letter of each of its words converted into capital. For example, if I have: "Hello world," it will become "H. W." with dot and a gap between them. Also, the function should return (return should be used, not print) the result for each sentence. – Vickycoding Dec 03 '17 at 17:50
  • Ah. So each *word* in a sentence? For example "My name is john" Will become "M.N.I.J"? – ICanKindOfCode Dec 03 '17 at 18:16
  • Exactly!! That's why I supposed I should have split as a method as well.. Don't know though – Vickycoding Dec 03 '17 at 18:21
  • @Vickycoding hope it is clear now. If this helped you, mark it as an accepted answer please :-) – ICanKindOfCode Dec 03 '17 at 18:38
  • Thank you really so much! Just a clarification: I run it and it shows: "name 'my_word' is not defined" in the line with the print – Vickycoding Dec 03 '17 at 19:01
  • and how do I press "accepted answer"? Thank you once again – Vickycoding Dec 03 '17 at 19:51
  • Whoops, sorry forgot to include the `my_word` variable in the answer. Sorry :'(. And you can press the green tick mark left of that answer to do so. In case anyone else has the same issue and stumbles here, it might help them :D – ICanKindOfCode Dec 04 '17 at 04:24