Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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 68,423 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']