You can do so by creating an array of dtype=object. If you try to assign a long string to a normal numpy array, it truncates the string:

>>> a = numpy.array(['apples', 'foobar', 'cowboy'])
>>> a[2] = 'bananas'
>>> a
array(['apples', 'foobar', 'banana'], 
      dtype='|S6')

But when you use dtype=object, you get an array of python object references. So you can have all the behaviors of python strings:

>>> a = numpy.array(['apples', 'foobar', 'cowboy'], dtype=object)
>>> a
array([apples, foobar, cowboy], dtype=object)
>>> a[2] = 'bananas'
>>> a
array([apples, foobar, bananas], dtype=object)

Indeed, because it's an array of objects, you can assign any kind of python object to the array:

>>> a[2] = {1:2, 3:4}
>>> a
array([apples, foobar, {1: 2, 3: 4}], dtype=object)

However, this undoes a lot of the benefits of using numpy, which is so fast because it works on large contiguous blocks of raw memory. Working with python objects adds a lot of overhead. A simple example:

>>> a = numpy.array(['abba' for _ in range(10000)])
>>> b = numpy.array(['abba' for _ in range(10000)], dtype=object)
>>> %timeit a.copy()
100000 loops, best of 3: 2.51 us per loop
>>> %timeit b.copy()
10000 loops, best of 3: 48.4 us per loop
Answer from senderle on Stack Overflow
Discussions

python - creating a numpy array filled with a character - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
How to mix strings and floats in a NumPy array?

I'd consider a pandas DataFrame instead - the actual data is stored as a numpy array and so supports everything it needs to, and the headers are separate so you don't need to do awkward slicing when working on the whole data set. You can also then index rows and columns by their index. And it supports going to and from CSVs easily.

As an aside, the way to get arrays to accept mixed types is

np.array([1, 'a'], dtype=object)

but obviously you lose practically all of the benefits of using numpy in the first place.

More on reddit.com
🌐 r/learnpython
8
6
January 25, 2015
Filling NumPy array with user input
Try this: import numpy as np arr = np.zeros((2, 3), dtype=int) for ind in np.ndindex(arr.shape): val = input(f"arr{list(ind)} = ") arr[ind] = int(val) print(arr) More on reddit.com
🌐 r/learnpython
12
4
August 11, 2024
How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
I can't say I fully followed your problem statement, but you can create an array with a total size of zero if any of the dimensions has size zero: a = np.empty((0, 3)) # Doesn't really matter if you use `empty`, `zeros` or `ones` here Zero-size arrays are the neutral element wrt. concatenation along their zero-size dimension (if that's what you mean by "merging"): b = np.random.uniform(size=(20, 3)) c = np.concatenate([a, b], 0) (c == b).all() # True More on reddit.com
🌐 r/learnpython
2
2
September 21, 2023
🌐
Python Forum
python-forum.io › thread-27443.html
filling and printing numpy arrays of str
I am new to python but experienced in several other languages. I do not understand yet why numpy.full does not fill an array of strings with the string value I supply in the code. Example code and out
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.6.dev0 Manual
Fill expects a scalar value and always behaves the same as assigning to a single array element. The following is a rare example where this distinction is important: >>> a = np.array([None, None], dtype=np.object_) >>> a[0] = np.array(3) >>> a array([array(3), None], dtype=object) >>> a.fill(np.array(3)) >>> a array([array(3), array(3)], dtype=object)
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › numpy › python fill array with value
How to Fill Array With Value in NumPy | Delft Stack
March 11, 2025 - It returns a new array with the specified shape, filled entirely with the given value. ... import numpy as np array_shape = (3, 4) fill_value = 7 filled_array = np.full(array_shape, fill_value) print(filled_array)
🌐
AskPython
askpython.com › python-modules › numpy › numpy-full
NumPy full() function - AskPython
September 29, 2026 - This is useful when an array will ... print(weights) print(weights.dtype) The fill value can be a string, boolean, or another value that NumPy can represent in the requested array type....
🌐
OpenSourceOptions
opensourceoptions.com › fill-numpy-arrays-with-numpy-fill-and-numpy-full
Fill NumPy Arrays with numpy.fill and numpy.full – OpenSourceOptions
May 1, 2021 - For example, to fill the array we created with the value 2, use the following line of code. ... Your result should match the one below. ... That’s all for numpy.fill() it’s a very simple, very powerful, and very useful function.
🌐
Medium
medium.com › @amit25173 › trick-to-quickly-numpy-create-array-with-same-value-5fac963d3921
Trick to Quickly numpy create array with same value | by Amit Yadav | Medium
April 12, 2025 - The easiest way to create an array filled with the same value is by using NumPy’s numpy.full() function. This function allows you to specify the size of the array and the value you want to fill it with.
🌐
NumPy
numpy.org › devdocs › user › basics.strings.html
Working with Arrays of Strings And Bytes — NumPy v2.6.dev0 Manual
Note that unlike fixed-width strings, StringDType is not parameterized by the maximum length of an array element, arbitrarily long or short strings can live in the same array without needing to reserve storage for padding bytes in the short strings.
🌐
Reddit
reddit.com › r/learnpython › filling numpy array with user input
r/learnpython on Reddit: Filling NumPy array with user input
August 11, 2024 -

Hello, everyone! I've been studying programming at school using Python for three years now, but would still consider myself very much a beginner. Earlier this year, we learned about NumPy arrays. We exclusively dealt with one-dimensional arrays filled with user input. I've been trying to figure out how to do the same for multi-dimensional arrays, but nothing I've found online resembles the method I was taught in class. Does anyone know if this method can apply to multi-dimensional arrays? If so, how?

Here's an example of it: (NumPy array filled with 4 integers fed by the user)

from numpy import array
arr=array(4*[int()])
for i in range (0,4):
    arr[i]=int(input("arr["+str(i)+"]="))