← flaviocopes.com

How to check if a variable is a string in Python

Published Mar 01 2021

⚠️⚠️ JUST A FEW HOURS LEFT to JOIN THE 2023 BOOTCAMP ⚠️⚠️

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


Wanna go from noobie to expert?

I wrote an entire book on this topic 👇

© 2023 Flavio Copes Flavio Copes using Notion to Site Notion to Site

Interested in solopreneurship?