+ 1
how to print type of a variable without <class> stuff ?
x=[1,2,3] print(type(x) #Expected output : list x="Hello World!" print(type(x)) #Expected output : String #and so on.... without if else clause because I am gonna use it to raise an exception with beautiful message..
3 Answers
+ 4
Use type(x).__name__.
Like this:
x=[1,2,3]
print(type(x).__name__)
Here's the code:
https://code.sololearn.com/ca24A12A13a1
But it returns str instead of string.
Here is the resource:
https://stackoverflow.com/questions/5008828/convert-a-JUMP_LINK__&&__python__&&__JUMP_LINK-type-object-to-a-string
+ 2
Though I would not recommend it, I found another way of doing this.
You can use .replace() method to achieve this.You can delete the <class ' and '> from the return type by converting it to str and using replace.Like this:
https://code.sololearn.com/ca22a8A23a1A
+ 1
print(str(type(x))[8:-2:])