Using Python 3. Which method is more Pythonic? Converting to uppercase:
guess = input("\n\nEnter your guess: ") guess = guess.upper() print(guess)
or
guess = input("\n\nEnter your guess: ").upper() print(guess)
or
guess = input("\n\nEnter your guess: ") print(guess.upper())
I'd also really like to know any other more efficient ways of writing this. Thanks, really appreciate the advice.
Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters.
upper() and . lower() string methods are self-explanatory. Performing the . upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.
What is the tolower() function in C? In C, the tolower() function is used to convert uppercase letters to lowercase. When an uppercase letter is passed into the tolower() function, it converts it into lowercase. However, when a lowercase letter is passed into the tolower() function, it returns the same letter.
Variables can only contain upper and lowercase letters (Python is case-sensitive) and _ (the underscore character). Hence, because we can't have spaces in variable names a common convention is to capitalize the first letter of every word after the first. For example, myName, or debtAmountWithInterest.
It may eventually depend upon what you will do with the value but, frankly, any of these solutions is equally pythonic almost in any situation. Just use any one of them and go ahead to the real beef :)
The first variant is probably better, as it invites to check user input value, which is a good security practice also in Python.
It is also more convenient when you debug the program.
Probably this may be helpful (when run at the interactive prompt):
import this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With