1

this is the assignment

Implement the fix_capilization() function. fix_capilization() has a string parameter and returns an updated string, where lowercase letters at the beginning of sentences are replaced with uppercase letters. fix_capilization() also returns the number of letters that have been capitalized. Call fix_capilization() in the print_menu() function, and then output the number of letters capitalized and the edited string. Hint 1: Look up and use Python functions .islower() and .upper() to complete this task. Hint 2: Create an empty string and use string concatenation to make edits to the string.

Ex: 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!

this is my code

def fix_capitalization(userString):
    capitalCount = 0
    editedString = ""
    for cap in userString.split("."):
        cap = cap.strip(" ").capitalize()
        capitalCount += 1
        editedString += cap + ". "
    return capitalCount, editedString

I am having a hard time concocting it together. I think I am needing to add a split with other punctuations besides the period, and when I concoct it together it just doesn't come out right. If I add the ". " it adds a period even on sentences that have an exclamation points, and if I get rid of the period, it just adds spaces and get's rid of the punctuation. What am I doing wrong?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Does this answer your question? [Capitalize the first letter after a punctuation](https://stackoverflow.com/questions/28639677/capitalize-the-first-letter-after-a-punctuation) – scandav Oct 18 '21 at 07:20
  • Thanks very much, that does make it more clear! I do need to be able to count how many letters I capitalized as well. – helpthisnewbie540 Oct 18 '21 at 07:59
  • You assume that all sentences are terminate by period ('.'). That's naive. What if the first letter of a sentence is already uppercase? There would be no need to convert or count it –  Oct 18 '21 at 08:11

1 Answers1

0

This sort of exercise can be a pig.
Does a sentence always end with a full-stop (period) and a space?
Often, but it might also end with a Question mark, or an Exlamation mark or it's the final sentence, where there is no following space.
You've also been restricted to the functions you can use and a method for constructing the return value. (I'm assuming they don't want you using regular expressions etc)

Here's a stab at a solution, feel free to break it, change it or disregard it.

def fix_capitalization(userString):
    use = ""
    cnt = 0
    tmp = userString.split(". ")
    for i in tmp:
        if any(e in i for e in ["!","?","."]): #  This should handle the final sentence ending in !,? or .
            if i[0].islower(): #  Check if the first character is lower case
                use += i[0].upper() + i[1:] #  Change the first character to upper case and append the rest
                cnt += 1 # Increment the count
            else:
                use += i
        else:
            if i[0].islower():
                use += i[0].upper() + i[1:]
                cnt += 1
            else:
                use += i
            use += ". " #  Add back the split .
    return use, cnt

fixed, cnt = fix_capitalization("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!")
print("Ex: Number of letters capitalized: ", cnt)
print(fixed)

Output:

Ex: Number of letters capitalized:  3
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!
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60