0
>>> print(type(float)==type(str))
True

Why is it true?

3
  • 3
    Because both float and str are types in Python, so both type(float) and type(str) result in type (the type of all type objects in Python). Commented Oct 1, 2020 at 13:58
  • 1
    It didn't occur to you to look at type(float) and type(str) and see what they are? Commented Oct 1, 2020 at 13:59
  • It's not clear where exactly you believe there is any type casting. Commented Oct 1, 2020 at 14:02

2 Answers 2

1

The function type did the following task:

With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.class.

str and float are built-in data types of python. So your code works like this:

print(type(str))   # <class 'type'>
print(type(float)) # <class 'type'>

If you want to get the desired result, you can use the following code:

s = "test"
f = 3.0
print(type(s)==type(f)) # false
Sign up to request clarification or add additional context in comments.

Comments

0

This happens because type(float) and type(str) are "type" objects

If you need to verify the type of a float value you can do something like this

float_value = 10.3
print(type(float_value) == float) # True

Another example with a string:

string_value = "hi"
print(type(string_value) == str) # True

Comments

Your Answer

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.