How to check if a variable is a string in Python
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
I wrote 21 books to help you become a better developer:
- HTML Handbook
- Next.js Pages Router Handbook
- Alpine.js Handbook
- HTMX Handbook
- TypeScript Handbook
- React Handbook
- SQL Handbook
- Git Cheat Sheet
- Laravel Handbook
- Express Handbook
- Swift Handbook
- Go Handbook
- PHP Handbook
- Python Handbook
- Linux Commands Handbook
- C Handbook
- JavaScript Handbook
- Svelte Handbook
- CSS Handbook
- Node.js Handbook
- Vue Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025