[Solved] Capitalization of the first letters of words in a sentence using python code [closed]


.split(sep):
Specifically split method takes a string and returns a list of the words in the string, using sep as the delimiter string. If no separator value is passed, it takes whitespace as the separator.

For example:

a = "This is an example"
a.split()        # gives ['This', 'is', 'an', 'example']  -> same as a.split(' ')

Talking about your function, it takes a sentence in the form of a string and returns the sentence with each word capitalized. Lets see it line by line:

def capitalize(sentence):    # sentence is a string
  w = sentence.split()       # splits sentence into words eg. "hi there" to ['hi', there']
  ww = ""                   # initializes new string to empty
  for word in w:                 # loops over the list w
    ww += word.capitalize() + " "      # and capitalizes each word and adds to new list ww
  return ww                               # returns the new list

solved Capitalization of the first letters of words in a sentence using python code [closed]