Search code examples
pythonstringsplitcapitalize

Capitalize the first word of a sentence in a text


I want to make sure that each sentence in a text starts with a capital letter.

E.g. "we have good news and bad news about your emissaries to our world," the extraterrestrial ambassador informed the Prime Minister. the good news is they tasted like chicken." should become

"We have good news and bad news about your emissaries to our world," the extraterrestrial ambassador informed the Prime Minister. The good news is they tasted like chicken."

I tried using split() to split the sentence. Then, I capitalized the first character of each line. I appended the rest of the string to the capitalized character.

  text = input("Enter the text: \n")
  lines = text.split('. ') #Split the sentences

  for line in lines:
      a = line[0].capitalize() # capitalize the first word of sentence
      for i in range(1, len(line)):
           a = a + line[i] 
      print(a)

I want to obtain "We have good news and bad news about your emissaries to our world," the extraterrestrial ambassador informed the Prime Minister. The good news is they tasted like chicken."

I get "We have good news and bad news about your emissaries to our world," the extraterrestrial ambassador informed the Prime Minister The good news is they tasted like chicken."


Solution

  • When you split the string by ". " that removes the ". "s from your string and puts the rest of it into a list. You need to add the lost periods to your sentences to make this work.

    Also, this can result in the last sentence to have double periods, since it only has "." at the end of it, not ". ". We need to remove the period (if it exists) at the beginning to make sure we don't get double periods.

    text = input("Enter the text: \n")
    output = ""
    if (text[-1] == '.'):
        # remove the last period to avoid double periods in the last sentence
        text = text[:-1] 
    lines = text.split('. ') #Split the sentences
    
    for line in lines:
        a = line[0].capitalize() # capitalize the first word of sentence
        for i in range(1, len(line)):
            a = a + line[i]
        a = a + '.' # add the removed period
        output = output + a
    print (output)
    

    We can also make this solution cleaner:

    text = input("Enter the text: \n")
    output = ""
    
    if (text[-1] == '.'):
        # remove the last period to avoid double periods in the last sentence
        text = text[:-1] 
    lines = text.split('. ') #Split the sentences
    
    for line in lines:
        a = line[0].capitalize() + line [1:] + '.'
        output = output + a
    print (output)
    

    By using str[1:] you can get a copy of your string with the first character removed. And using str[:-1] will give you a copy of your string with the last character removed.