Invoke the built-in help system with python's dir and help methods

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet

Dir and help are two must-know functions in Python. Dir will provide us with a list of methods available on the object of interest while help will inspect a specific method we would like to use.

Learning about them in this lesson will help you understand what functions and methods are available for use as well has how to use them.

I have my string foo here, and I need to convert it to upper case letters. I know that's possible, but instead of going to Stack Overflow or Google, I can use the dir method in Python and provide the object in question. Whenever I run that, it gives me a list of everything that's possible with that object. It knows that this is a string, so it only gives me string-based operations.

In the output here, the first thing you see are these double under methods. They're called double unders because they start with a double underscore. These methods aren't intended for you to call them directly, but they do show you the possible operations. For example, if I wanted to add foo plus bar, that actually uses the __add method, but I used the plus symbol instead of calling it directly.

Back to my current problem though, of how to convert it to upper case, I look at the rest of the methods here and there's one called upper. Sounds like what I need.

To see how to use it, I can call Python's built-in help system and provide my string plus the method that I'm trying to figure out how to use. That opens up the built-in help menu, it shows me that if I call the string with the .upper method, it'll return a copy of the string converted to upper case. Let's try that out. I'll say foo.upper, and there it is, all upper case letters.