Sean Mabli
Sean Mabli

Reputation: 52

Python deleting 0 at end of string

I am tring to transfer a numpy array of two strings to another numpy array. But when every on of the strings has a value of 10 it transfers a 1.

Code:

import numpy as np

x= np.full(2, '', dtype=str)

y = np.array([['10', 'C']])

print(y[0, 0])
x[0] = y[0, 0]
print(x[0])

Output:

10
1

Upvotes: 0

Views: 85

Answers (1)

Barmar
Barmar

Reputation: 780994

Numpy strings have fixed length, so you need to specify the length in the dtype. dtype=str defaults to length = 1. As a result, any strings are truncated to the first character.

x= np.full(2, '', dtype='<U10')

Specifies the datatype as 10-character Unicode strings.

Upvotes: 1

Related Questions