[Numpy-discussion] Empty strings not empty?

Matthew Brett matthew.brett at gmail.com
Tue Dec 29 18:35:55 EST 2009


Hi,

I was surprised by this - should I have been?

In [35]: e = np.array(['a'])

In [36]: e.shape
Out[36]: (1,)

In [37]: e.size
Out[37]: 1

In [38]: e.tostring()
Out[38]: 'a'

In [39]: f = np.array(['a'])

In [40]: f.shape == e.shape
Out[40]: True

In [41]: f.size == e.size
Out[41]: True

In [42]: f.tostring()
Out[42]: 'a'

In [43]: z = np.array(['\x00'])

In [44]: z.shape
Out[44]: (1,)

In [45]: z.size
Out[45]: 1

In [46]: z
Out[46]:
array([''],
      dtype='|S1')

That is, an empty string array seems to be the same as a string array
with a single 0 byte, including having shape (1,) and size 1...

Best,

Matthew



More information about the NumPy-Discussion mailing list