Home > database >  How do I capitalize the letter immediately after a period?
How do I capitalize the letter immediately after a period?

Time:08-10

For my assignment, I have to implement the fix_capitalization() function. fix_capitalization() has a string parameter and returns an updated string, where lowercase letters at the beginning of sentences are replaced with uppercase letters. fix_capitalization() also returns the number of letters that have been capitalized. Call fix_capitalization() in the execute_menu() function, and then output the number of letters capitalized followed by the edited string. Example: Original text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! Number of letters capitalized: 3 Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

My code so far:

def fix_capitalization(usr_str):
    small_char = usr_str.split('.')
    return small_char

Usr_str is the variable for the input. Any help is appreciated. Thank you.

CodePudding user response:

You can use regex, this way, multiple spaces can be handled:

import re
usr_str = "we'll continue our quest in space.  there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!"
usr_str, n = re.subn("(^|[.])\s*[a-zA-Z]", lambda x: x.group(0).upper(), usr_str)
print(usr_str)
print(n)

Output:

We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
3

And from your approach, I can think of this, which would only work for single space:

usr_str = usr_str.split('. ')
n = len(usr_str)
print('. '.join([s.capitalize() for s in usr_str])) 

CodePudding user response:

As Ash stated you can use regex to handle multiple spaces and for a single space with capitalized count you can just initialize a variable and count whenever you capitalized after the period

def fix_capitalization(text):
    # Split the text into a list of sentences.
    sentences = text.split(". ")
    # Initialize a variable to hold the number of capitalized letters.
    capitalized = 0
    # Loop through the sentences.
    for i in range(len(sentences)):
        # If the first letter of the sentence is lowercase, capitalize it.
        if sentences[i][0].islower():
            sentences[i] = sentences[i].capitalize()
            capitalized  = 1
    # Join the sentences back together into a single string.
    text = ". ".join(sentences)
    # Return the number of capitalized letters and the updated text.
    return capitalized, text

CodePudding user response:

You can also do it this way, perhaps you find it easier:

usr_str = "we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!"

usr_str_nospace = usr_str.replace(". " ,".")
texts = usr_str_nospace.split('.')
texts = [text.capitalize() for text in texts]
user_str_capitalized = ". ".join(texts)
print("Capital Letters: ", sum(1 for c in user_str_capitalized if c.isupper()))
print(user_str_capitalized)
  • Related