+ 5
How do I convert a list elements from uppercase to lowercase or vice versa?
I made a guessing game as shown below. Right now, the first letter of all elements of fruits list are in uppercase. So, we have to provide the input as it is. But, I would like to change this code to accept both upper and lower letters. I know we can do this using for loop and things. Is it possible to do this without them? https://code.sololearn.com/c3C0oq8WF322/?ref=app
5 Réponses
+ 13
The fruits in the list all begin with a capital letter. You can ensure the input is capitalized the same way by using a built-in string method named capitalize(). Change:
guess = input()
To:
guess = input().capitalize()
+ 7
use .lower() to convert to lower case and .upper() to convert to upper case
+ 5
Brian perfect!
Thank you!
Actually, I was fighting with lower() method and I could guess that there should be something else.
Also, we can use title() method for those wondering.
guess = input().title()
+ 1
Thanks!
(P.S. Is .upper() and .lower() actual python code?)
0
FoxCatGirl639 Yes. They are methods built in to the string object. You may learn about these and other string methods here: https://docs.python.org/3/library/stdtypes.html#string-methods