Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Yosef Fastow
Yosef Fastow
18,526 Points

How do you print the name of a variable or list without printing the value?

How do you print the name of a variable or list without printing the value? For example if: name = "Hello World!" how do I get "name" printed instead of "Hello world!" ?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

As you create variables, their names get added to the global namespace. You can see the global namespace using dir()

Python 3.3.0 (default, Nov 26 2015, 16:04:52) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on unknown
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__']
>>> name = "Hello World!"
>>> name
'Hello World!'
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', 'name']