Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Why .upper change type of string

I have a problem with my hangman project. When matching user input (lower case) with the word to guess (upper case), I’m using .upper which is causing an error.

if user_input.upper in self.word_to_guess:
                indexes = self.find_indexes(user_input)
                self.update_progress(user_input, indexes)

                # If there is no letter to find in the word

                if self.game_progress.count('_') == 0:
                    print('\n¡Yay! You win!')
                    print('The word is: {0}'.format(self.word_to_guess))
                    quit()

and I test that .upper change <class ‘str’> to <class ‘builtin_function_or_method’>

M = 'me'
print(M)
print(type(M))
print('')

M =(M.upper)
print(str(M))
print(type(M))
print('')

result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

me
<class 'str'>

<built-in method upper of str object at 0x00007FFA191B1200>
<class 'builtin_function_or_method'>

I can convert those words to lowercase. However, I need to know how to use .upper() to change only ‘me’ to ‘ME’, without changing it into a class.’builtin_function_or_method’

>Solution :

You are not calling the method, but referencing it instead.

By doing M.upper you are making M equal to the upper method instead of actually running it.

M.upper() will run the method and return the result

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading