What function do you use to check what kind of datatype your variable or parameter
Asked
Active
Viewed 288 times
-2
-
isinstance?.... – vks Aug 17 '22 at 19:18
-
`type("") == str`. However, this may not be necessary/wise/etc. – Fiddling Bits Aug 17 '22 at 19:18
-
see https://stackoverflow.com/questions/4843173/how-to-check-if-type-of-a-variable-is-string – SargeATM Aug 17 '22 at 19:19
-
Does this answer your question? [How to check if type of a variable is string?](https://stackoverflow.com/questions/4843173/how-to-check-if-type-of-a-variable-is-string) – SargeATM Aug 17 '22 at 19:21
-
@FiddlingBits **no** that is absolutely not how you should be doing that – juanpa.arrivillaga Aug 17 '22 at 20:12
2 Answers
1
In Python, the recommended way is to do it with isinstance(), so to see the type of var:
isinstance(var, str)
Alternatively, you can do it with type():
type(var)
Using the second way, in a more specific context, if you want to know if var is a string, you can do it this way:
type(var) == str

Dennis Kozevnikoff
- 2,078
- 3
- 19
- 29
-
-
What do you mean? My code is correct, and it works, however, if you think it can be improved please edit it as you see fit. – Dennis Kozevnikoff Aug 17 '22 at 20:39
-
I said what I meant, it is not idiomatic. Idiomatically, you would do `isinstance(var, str)`. – juanpa.arrivillaga Aug 17 '22 at 20:56
-
Thanks for bringing it to my attention, i edited the answer. – Dennis Kozevnikoff Aug 17 '22 at 21:59