DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Lakshya Tyagi
Lakshya Tyagi

Posted on

Correct syntax to output the type of variable or object?

  1. print(type(x))
  2. print(typeof(x))
  3. print(typeof x)
  4. print(typeOf(x))

Write your answer in comment

Top comments (9)

Collapse
 
dwd profile image
Dave Cridland • Edited on

Well, obviously you could use print(type(x)), but that's not very useful, since type doesn't return a string but a class, and then print will call str() on it, so you typically and up with the repr() of the class:

print(type(''))  # <class 'str'>
Enter fullscreen mode Exit fullscreen mode

What you can do is hit the introspection bits, for example:

x = ''
print(''.join((x.__class__.__module__, '.', x.__class__.__name__)))  # builtins.str
Enter fullscreen mode Exit fullscreen mode

Which is a bit prettier.

Collapse
 
egrep profile image
Serghei Iakovlev

str.join() takes exactly one argument (3 given) :)

Collapse
 
dwd profile image
Dave Cridland

Nice catch - I edited to pass in a tuple rather than the args directly.

Thanks for the demonstration that good code review is important!

Collapse
 
iesouskurios profile image
J Wylie

print(type(x))

Collapse
 
envoy_ profile image
Vedant Chainani

print(type(x))

Collapse
 
mukeshgurpude profile image
Mukesh Gurpude
  1. print(type(x))
Collapse
 
dibyadarshan8 profile image
Dibyadarshan Rath

1

Collapse
 
promikecoder2020 profile image
ProMikeCoder2020 • Edited on

The correct answer is the answr number one

Collapse
 
mehrnoosh profile image
Mehrnoosh H. Kashani

1
print(type(x))

Let's Get Wacky


Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

β†’ Join the Hackathon <-