Check If Type of a Variable is String in Python

A string is a series of characters.

A character is nothing more than a representation of something. For example, there are 26 characters in the English language.

Computers do not work with characters, but rather with numbers (binary). Although characters are seen on your screen, they are stored and manipulated internally in a series of 0’s and 1’s.

A string in Python is a sequence of Unicode characters. Unicode was created in order to include every character in every language and to bring encoding uniformity.. Python Unicode can teach you everything you need to know about Unicode.

Examples:

Input:

string="hello"

Output:

Yes it is string variable

Determine whether a variable type is string

There are several ways to check whether a variable type is string some of them are:

Method #1 : Using type() function

The type() method returns the class type of the argument (object) that was passed as a parameter. The function type() is used mainly for debugging.

The type() function accepts two types of arguments: single and three argument. If only one argument is passed, type(obj), it returns the type of the given object. If type(name, bases, dict) is passed as an argument, it returns a new type object.

Syntax:

type(object)
type(name, bases, dict)

Parameters:

name : class name, which corresponds to the class’s __name__ attribute.
bases: a tuple of classes from which the current class is descended. Later corresponds to the attribute __bases__.
dict: a dictionary containing the class’s namespaces. Later is equivalent to the __dict__ attribute.

Return:

returns a new type class or, more precisely, a metaclass

We get the type of variable using type() and then compare it to str to see if it is a string variable.

Below isĀ  the implementation:

# given variable
variable = "hello"
# comparing str and type of variable
if(type(variable) == str):
    print("Yes it is string variable")
else:
    print("No it is not string variable")

Output:

Yes it is string variable

Method #2 : Using isinstance() function

Python comprises a function for determining the type of a variable.

isinstance(object, classinfo)

The result of this function is If the given object is an instance of class classinfo or any of its subclasses, return True otherwise, return False.

Let’s see if we can use this to see if a variable is of the string form.

Below is the implementation:

# given variable
variable = "hello"
# using isinstance function to check whether the type of it is string or not
if(isinstance(variable, str)):
    print("Yes it is string variable")
else:
    print("No it is not string variable")

Output:

Yes it is string variable

Method #3 : By comparing types

We hard coded the string class in both previous solutions. But we can also compare the type of variable with the type of the empty string, without hard coding.

We know that the string object type is “”.

To compare the types we use this.

Below is the implementation:

# given variable
variable = "hello"
# comparing using type() function as type of string is ""
if(type(variable) == type("")):
    print("Yes it is string variable")
else:
    print("No it is not string variable")

Output:

Yes it is string variable

Related Programs: