Return the string representation of a scalar dtype in Python

To return the string representation of a scalar dtype, use the sctype2char() method in NumPy. If a scalar dtype is provided, the corresponding string character is returned. If an object is passed, sctype2char() tries to infer its scalar type and then return the corresponding string character.

Syntax

numpy.sctype2char(sctype)

Parameters:

  • sctype ? A scalar dtype or an object from which the dtype can be inferred

Returns: A single character string representing the scalar type

Basic Usage

At first, import the required library ?

import numpy as np

# The string representation of common scalar types
print("Common scalar types:")
for dtype in [np.int32, np.double, np.complex_, np.string_, np.ndarray]:
    print(f"{dtype.__name__}: {np.sctype2char(dtype)}")
Common scalar types:
int32: i
float64: d
complex128: D
bytes_: S
ndarray: O

Integer Types

Return the string representation of different integer types ?

import numpy as np

print("Integer type representations:")
for dtype in [np.int16, np.int32, np.int64]:
    print(f"{dtype.__name__}: {np.sctype2char(dtype)}")
Integer type representations:
int16: h
int32: i
int64: l

Float Types

Return the string representation of different float types ?

import numpy as np

print("Float type representations:")
for dtype in [np.float16, np.float32, np.float64]:
    print(f"{dtype.__name__}: {np.sctype2char(dtype)}")
Float type representations:
float16: e
float32: f
float64: d

Character Meanings

Character Type Description
h int16 Short integer
i int32 Integer
l int64 Long integer
e float16 Half precision float
f float32 Single precision float
d float64 Double precision float
D complex128 Complex number
S string Byte string
O object Object type

Conclusion

The sctype2char() function provides a quick way to get single-character representations of NumPy scalar types. This is useful for identifying data types programmatically and understanding NumPy's internal type system.

Updated on: 2026-03-26T19:16:52+05:30

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements