Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
m a beginner and am having trouble with this question: Create a function that receives a string input from the user and returns the number of upper case letters, lower case letters, as well as converting the string to Title Case. The input and print commands should be part of the main program.

should use str.islower(), str.isupper(), str.istitle()

What I have tried:

#Enter user input

string = input("Please enter a string: ")
def count(string):

    d = {"upper":0, "lower":0} #Monitor upper and lower case count
    for str in string:
        if str.isupper():
           d["upper"]+=1
        elif str.islower():
           d["lower"]+=1
        else:
           pass
Posted
Updated 14-Jul-20 8:18am

1 solution

Read the question again: it want's you to count them, and convert the string to Title Case - i.e. An Upper Case Letter At The Start Of Each Word.

So you need to do two things: Count upper and lower, and convert the string. Remember that Python strings are immutable, so you can't alter the input string, you need to assemble a new output string as you go, copyying each character to the output at the end of your loop.

Hint: create a "at start of word" flag, start it as TRUE, and set it TRUE again when you meet a space. Otherwise, if it is upper case or not a letter, set it to FALSE. if it is lowercase, check the flag, and uppercase it if necessary before setting it FALSE.
 
Share this answer
 
Comments
CPallini 15-Jul-20 2:03am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900