Back
Is there a way to check if the type of a variable in python is string.. like
isinstance(x,int);
for integer values?
The correct way of solving this problem is:-
isinstance(s, str)
You can also solve this problem in the following way:-
if type(x) == str:
Another thing you can do is:-
var = 1 if type(var) == int: print('your variable is an integer')
var = 1
if type(var) == int:
print('your variable is an integer')
Or:
var2 = 'this is variable'if type(var2) == str: print('your variable is a string')else: print('your variable IS NOT a string')
var2 = 'this is variable'
if type(var2) == str:
print('your variable is a string')
else:
print('your variable IS NOT a string')
I hope these answers will help you in clearing your doubts regarding this question.
31k questions
32.8k answers
501 comments
693 users