+ 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..

28th Jun 2021, 4:40 PM
Ratnapal Shende
Ratnapal Shende - avatar
3 odpowiedzi
+ 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
28th Jun 2021, 4:54 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 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
28th Jun 2021, 5:27 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
print(str(type(x))[8:-2:])
28th Jun 2021, 8:08 PM
Jayakrishna 🇮🇳