How to check if a variable is a string in Python
New Courses Coming Soon
Join the waiting lists
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
→ Get my Python Handbook
→ Get my Python Handbook
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- THE VALLEY OF CODE your web development manual
- BOOKS 17 coding ebooks you can download for free on JS Python C PHP and lots more
- Interesting links collection
- Follow me on X