You can check if a variable is a string using the type()
function, passing the variable as an argument, and then comparing the result to the str
class:
name = "Roger"
type(name) == str #True
Or using isinstance()
, passing 2 arguments: the variable, and the str
class:
name = "Roger"
isinstance(name, str) #True
Download my free Python Handbook
More python tutorials:
- Python Tuples
- Python List comprehensions
- Python Lambda Functions
- Python Ternary Operator
- Python Enums
- Python Strings
- Python, Accepting Input
- Python Decorators
- Python Booleans