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
GUMMADI VIJAYA KUMAR
213 silver badges12 bronze badges
-
Would you please clarify what is the expected output? This looks correct to me.Aziz– Aziz2020年05月31日 15:04:57 +00:00Commented May 31, 2020 at 15:04
-
So you want to store a type object in a stringDaVinci– DaVinci2020年05月31日 15:09:21 +00:00Commented 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 yesGUMMADI VIJAYA KUMAR– GUMMADI VIJAYA KUMAR2020年05月31日 15:09:27 +00:00Commented May 31, 2020 at 15:09
1 Answer 1
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
Aziz
20.9k8 gold badges68 silver badges71 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
GUMMADI VIJAYA KUMAR
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.
Aziz
@GUMMADIVIJAYAKUMAR I added more clarification to the answer.
lang-py