pythonpython-3.xstringnumpydtype

Why full function in NumPy can't take dtype=str


Why i see just one letter instead of "cube" When i didn't type dtype="str" it worked. My question is why?

code:

np.full((3,3,3),"cube",dtype=str)

results:

array([[['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']]], dtype='<U1')

Solution

  • np.full((3,3,3),"cube",dtype=str)
    

    is equivalent to

    np.full((3,3,3),"cube",dtype='U1')
    

    It will just take the first element of the string.


    You can get full result by:

    np.full((3,3,3),"cube",dtype=object)
    

    or

    np.full((3,3,3),"cube",dtype='U4')  # U4 as you have 4 letters in cube, you can do any U>4 like U5, U6, etc.
    

    or just remove dtype=str. Upon removing it will take the complete length of the string.

    np.full((3,3,3),"cube")