Return the string representation of a scalar dtype in Python


To return the string representation of a scalar dtype, use the sctype2char() method in Python Numpy. The 1st argument, if a scalar dtype, the corresponding string character is returned. If an object, sctype2char tries to infer its scalar type and then return the corresponding string character.

Steps

At first, import the required library −

import numpy as np

The string representation of a scalar type −

for i in [np.int32, np.double, np.complex_, np.string_, np.ndarray]:
   print(np.sctype2char(i))

Return the string representation of int types −

print("\nThe string representation of int types...")
for j in [np.int16, np.int32,np.int64]:
   print(np.sctype2char(j))

Return the string representation of float types −

print("\nThe string representation of float types...")
for j in [np.float16, np.float32,np.float64]:
   print(np.sctype2char(j))

Example

import numpy as np

# To return the string representation of a scalar dtype, use the sctype2char() method in Python Numpy
# The 1st argument, if a scalar dtype, the corresponding string character is returned.
# If an object, sctype2char tries to infer its scalar type and then return the corresponding string character.
print("The string representation of a scalar type...")
for i in [np.int32, np.double, np.complex_, np.string_, np.ndarray]:
   print(np.sctype2char(i))

# Return the string representation of int types
print("\nThe string representation of int types...")
for j in [np.int16, np.int32,np.int64]:
   print(np.sctype2char(j))

# Return the string representation of float types
print("\nThe string representation of float types...")
for j in [np.float16, np.float32,np.float64]:
   print(np.sctype2char(j))

Output

The string representation of a scalar type...
i
d
D
S
O

The string representation of int types...
h
i
l

The string representation of float types...
e
f
d

Updated on: 25-Feb-2022

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements