Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting to upper case. Which way is more pythonic? [closed]

Tags:

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.

like image 852
Zefi Avatar asked Jan 07 '12 18:01

Zefi


People also ask

What is the correct way to convert a string into upper case?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters.

What does change uppercase to in Python?

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.

Which function is used to convert all upper case letters in a string to lower case letters?

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.

Does upper and lower case matter in Python?

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.


2 Answers

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 :)

like image 173
brandizzi Avatar answered Oct 20 '22 21:10

brandizzi


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 
like image 40
Roman Susi Avatar answered Oct 20 '22 21:10

Roman Susi