Skip to content

Python if not String | Example code

  • by

There are 2 ways to check is NOT a string in python. First, use the isinstance() function, and uses the type function with an if statement.

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. You 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 or suggestions on this Python string topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading