How to check if type of a variable is string in Python? (2024)

How to check if type of a variable is string in Python? (1)

  • Trending Categories
  • Data Structure
  • Networking
  • RDBMS
  • Operating System
  • Java
  • MS Excel
  • iOS
  • HTML
  • CSS
  • Android
  • Python
  • C Programming
  • C++
  • C#
  • MongoDB
  • MySQL
  • Javascript
  • PHP
  • Physics
  • Chemistry
  • Biology
  • Mathematics
  • English
  • Economics
  • Psychology
  • Social Studies
  • Fashion Studies
  • Legal Studies
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

In this article, we are going to find out how to check if the type of a variable is a string in Python.

The first approach is by using the isinstance() method. This method takes 2 parameters, the first parameter being the string that we want to test and the next parameter is the keyword str. This method will return True if the given input is a string, otherwise, it returns False.

It specifies that when we pass an object and class, or tuple of classes, to the isinstance() method, it will return True if the object's data type matches the class provided and False otherwise.

Example 1

In the example given below, we are taking input and checking whether it is a string or not using the isinstance() method and printing if the input is a string or not

str1 = "Tutorialspoint"print("The given string is")print(str1)print("Checking if the given input is string or not")print(isinstance(str1, str))

Output

The output of the above example is as shown below −

The given string isTutorialspointChecking if the given input is string or notTrue

Example 2

In the example given below, we are taking the same program as above but with different input and checking the type of the input and printing if the input is a string or not.

str1 = 10print("The given string is")print(str1)print("Checking if the given input is string or not")print(isinstance(str1, str))

Output

The output of the above example is given below −

The given string is10Checking if the given input is string or notFalse

Using type() method

The second approach is by using the inbuilt method type(). This method takes an input and returns the type of the given input. We will return True if the type is string otherwise return False.

Example 1

In the example given below, we are taking an input and checking if the given input is string or not using type() method and printing if the input is a string or not

str1 = "Tutorialspoint"print("The given string is")print(str1)print("Checking if the given input is a string or not")print(type(str1) == str)

Output

The output of the above example is as shown below −

The given string isTutorialspointChecking if the given input is a string or notTrue

Example 2

In the example given below, we are taking the same program as above but we are taking a different input and we are checking if it belongs to string or not

str1 = 10print("The given string is")print(str1)print("Checking if the given input is string or not")print(type(str1) == str)

Output

The output of the above example is given below −

The given string is10Checking if the given input is string or notFalse

Tarun Chandra

Updated on: 07-Dec-2022

28K+ Views

  • Related Articles
  • How can I check if a JavaScript variable is function type?
  • How to check if String value is Boolean type in java?
  • Check if variable is tuple in Python
  • How to check if a string is alphanumeric in Python?
  • How to check if a string in Python is in ASCII?
  • How to check if a variable is NaN in JavaScript?
  • How to check if a variable is boolean in JavaScript?
  • How to check if a string is a valid keyword in Python?
  • How to check the type of a variable or object in JavaScript?
  • How to check if a string contains only one type of character in R?
  • How do I check if a Python variable exists?
  • How to check if a variable is NULL in C/C++?
  • How to check if a variable is an array in JavaScript?
  • How to check if a variable is an integer in JavaScript?
  • Check if a string is Colindrome in Python
Kickstart Your Career

Get certified by completing the course

Get Started

How to check if type of a variable is string in Python? (31)

Advertisem*nts

';

How to check if type of a variable is string in Python? (2024)
Top Articles
What is data sharing? Everything you need to know
How To Use Blockchain To Store Data [Multiple Options Available] | upGrad blog
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 6500

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.