0
How do we get to know when any number we print is an integer or string?
Like print("42") here 42 is a string as told in the tutorial how do we get to know that.
5 Answers
+ 3
Hi RAHUL!
Always remember, anything inside the quotes is a string. It can be an integer or text.
For example,
a = 42 is an integer
a = "42" is a string
b = 42.0 is a float(decimals)
b = "42.0" is a string
+ 2
U can use print(type(21))
Output <class 'int'>
Int is the type
+ 1
a="42"
print(type(a),type(a)==str,isinstance(a,str))
+ 1
'''
this function will return the type of it
it also kindly prints it out for debugging purposes
'''
def print_what_this_is(this,):
print(this.__class__.__name__)
return this.__class__.__name__
#examples
#run it
print_what_this_is(2)
print_what_this_is('uwu')
print_what_this_is([])
0
U can simply use type(x) to get the type of x.
Type(True) will give boolean
Type(1) will give int
Type ("46") Will give string
So on