Capitalization of each sentence in a string in Python 3

18,682

Solution 1

You are trying to use a string method on the wrong object; words is list object containing strings. Use the method on each individual element instead:

words2 = [word.capitalize() for word in words]

But this would be applying the wrong transformation; you don't want to capitalise the whole sentence, but just the first letter. str.capitalize() would lowercase everything else, including the J in Joe:

>>> 'my name is Joe'.capitalize()
'My name is joe'    

Limit yourself to the first letter only, and then add back the rest of the string unchanged:

words2 = [word[0].capitalize() + word[1:] for word in words]

Next, a list object has no .join() method either; that too is a string method:

string2 = '. '.join(words2)

This'll join the strings in words2 with the '. ' (full stop and space) joiner.

You'll probably want to use better variable names here; your strings are sentences, not words, so your code could do better reflecting that.

Together that makes your function:

def sentenceCapitalizer (string1: str):
    sentences = string1.split(". ")
    sentences2 = [sentence[0].capitalize() + sentence[1:] for sentence in sentences]
    string2 = '. '.join(sentences2)
    return string2

Demo:

>>> def sentenceCapitalizer (string1: str):
...     sentences = string1.split(". ")
...     sentences2 = [sentence[0].capitalize() + sentence[1:] for sentence in sentences]
...     string2 = '. '.join(sentences2)
...     return string2
... 
>>> print (sentenceCapitalizer("hello. my name is Joe. what is your name?"))
Hello. My name is Joe. What is your name?

Solution 2

This does the job. Since it extracts all sentences including their trailing whitespace, this also works if you have multiple paragraphs, where there are line breaks between sentences.

import re

def sentence_case(text):
    # Split into sentences. Therefore, find all text that ends
    # with punctuation followed by white space or end of string.
    sentences = re.findall('[^.!?]+[.!?](?:\s|\Z)', text)

    # Capitalize the first letter of each sentence
    sentences = [x[0].upper() + x[1:] for x in sentences]

    # Combine sentences
    return ''.join(sentences)

Here is a working example.

Share:
18,682
AndyM3
Author by

AndyM3

Updated on June 14, 2022

Comments

  • AndyM3
    AndyM3 almost 2 years

    This should be easy but somehow I'm not quite getting it.

    My assignment is:

    Write a function sentenceCapitalizer that has one parameter of type string. The function returns a copy of the string with the first character of each sentence capitalized. The function should return “Hello. My name is Joe. What is your name?” if the argument to the function is “hello. my name is Joe. what is your name?” Assume a sentence is separated by a period followed by a space."

    What I have so far is:

    def sentenceCapitalizer (string1: str):
        words = string1.split(". ")
        words2=words.capitalize()
        string2=words2.join()
        return (string2)
    
    print (sentenceCapitalizer("hello. my name is Joe. what is your name?"))
    

    Upon execution I get the error:

    Traceback (most recent call last):
      File "C:\Users\Andrew\Desktop\lab3.py", line 83, in <module>
        print (sentenceCapitalizer("hello. my name is Joe. what is your name?"))
      File "C:\Users\Andrew\Desktop\lab3.py", line 79, in sentenceCapitalizer
        words2=words.capitalize()
    AttributeError: 'list' object has no attribute 'capitalize'"
    

    What is that telling me and how do I fix this? I tried following instructions found on a page listed as the python software foundation so I thought I'd have this.