1

I have a code line

emp_id=1 
tp = type(emp_id)
print(tp)
print(type(tp))
strg = str(tp)
print(strg)
print(type(strg))

The result is as below

<class 'int'>
<class 'type'>
<class 'int'>
<class 'str'> 

**What i need is i want to store in a string.

How to do it? **

asked May 31, 2020 at 15:00
3
  • Would you please clarify what is the expected output? This looks correct to me. Commented May 31, 2020 at 15:04
  • So you want to store a type object in a string Commented May 31, 2020 at 15:09
  • What i need is i want to store class 'int' only in the strg with out angular quotes.@Tonystark yes Commented May 31, 2020 at 15:09

1 Answer 1

5

The function type(x) returns the class of which the object x is an instance. All classes in python have the property __name__ which returns the actual name (as a string) of that class.

x = 1
tp = type(x).__name__
print(tp)

This will print: int

answered May 31, 2020 at 15:11
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent i got what i need.@Aziz i need the explanation of what does __ name __ consists and how does it eliminated <class > from the result.If possible pelase explain in laymens terms.
@GUMMADIVIJAYAKUMAR I added more clarification to the answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.