Python if not String | Example code

There are 2 ways to check is NOT string in python. First using isinstance() function and another one is use type function with an if satatament.

Python if not String example

Simple example code.

Using isInstance()

By giving the second argument as “str” Check if Not string, This method can be used to test whether any variable is a particular datatype.

s = "Hello"

if not isinstance(s, str):
    print("Given var not a String")
else:
    print("It's a string")

Output:

Python if not String

Using type()

if the type is Not string python. Just need to pass the variable and equate it with a particular type.

s = 10

if not type(s) == str:
    print("Given var, not a String")
else:
    print("It's a string")

Output: Given var, not a String

Do comment if you have any doubts and suggestions on this Python string topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.