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
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.strings.html
Working with Arrays of Strings And Bytes โ€” NumPy v2.5.dev0 Manual
Working with data loaded or memory-mapped from a data file, where one or more of the fields in the data is a string or bytestring, and the maximum length of the field is known ahead of time. This often is used for a name or label field. Using NumPy indexing and broadcasting with arrays of Python strings of unknown length, which may or may not have data defined for every value.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.chararray.html
numpy.chararray โ€” NumPy v1.26 Manual
The chararray class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of dtype object_, bytes_ or str_, and use the free functions in the numpy.char module for ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ user โ€บ basics.strings.html
Working with Arrays of Strings And Bytes โ€” NumPy v2.1 Manual
Working with data loaded or memory-mapped from a data file, where one or more of the fields in the data is a string or bytestring, and the maximum length of the field is known ahead of time. This often is used for a name or label field. Using NumPy indexing and broadcasting with arrays of Python strings of unknown length, which may or may not have data defined for every value.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.strings.html
Working with Arrays of Strings And Bytes โ€” NumPy v2.4 Manual
Working with data loaded or memory-mapped from a data file, where one or more of the fields in the data is a string or bytestring, and the maximum length of the field is known ahead of time. This often is used for a name or label field. Using NumPy indexing and broadcasting with arrays of Python strings of unknown length, which may or may not have data defined for every value.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ string-functions
NumPy String Functions (With Examples)
import numpy as np # create two arrays of strings array1 = np.array(['C', 'Python', 'Swift']) array2 = np.array(['C++', 'Python', 'Java']) # check if each element of the arrays is equal result = np.char.equal(array1, array2) print(result) # Output: [False True False]
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.fromstring.html
numpy.fromstring โ€” NumPy v2.4 Manual
A new 1-D array initialized from text data in a string. ... A string containing the data. ... The data type of the array; default: numpy.float64. For binary input data, the data must be in exactly this format.
๐ŸŒ
New York University
physics.nyu.edu โ€บ pine โ€บ pymanual โ€บ html โ€บ chap3 โ€บ chap3_arrays.html
3. Strings, Lists, Arrays, and Dictionaries โ€” PyMan 0.9.31 documentation
The most import data structure for scientific computing in Python is the NumPy array. NumPy arrays are used to store lists of numerical data and to represent vectors, matrices, and even tensors. NumPy arrays are designed to handle large data sets efficiently and with a minimum of fuss.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.25 โ€บ reference โ€บ routines.char.html
String operations โ€” NumPy v1.25 Manual
The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_. For example ยท >>> np.char.capitalize(["python", "numpy"]) array(['Python', 'Numpy'], dtype='<U6') >>> np.char.add(["num", "doc"], ["py", "umentation"]) array(['numpy', ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ routines.char.html
Legacy fixed-width string functionality โ€” NumPy v2.4 Manual
The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_. For example ยท Try it in your browser! >>> import numpy as np >>> np.char.capitalize(["python", "numpy"]) array(['Python', 'Numpy'], dtype='<U6') >>> np.char.add(["num", "doc"], ...
๐ŸŒ
Awkward-array
awkward-array.org โ€บ doc โ€บ main โ€บ user-guide โ€บ how-to-create-strings.html
How to create arrays of strings โ€” Awkward Array 2.9.0 documentation
awkward_array = ak.Array(numpy_array) awkward_array ยท ['one', 'two', 'three', 'four'] --------- backend: cpu nbytes: 84 B type: 4 * string
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ routines.strings.html
String functionality โ€” NumPy v2.1 Manual
The numpy.strings module provides a set of universal functions operating on arrays of type numpy.str_ or numpy.bytes_.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.array2string.html
numpy.array2string โ€” NumPy v2.4 Manual
Callables should return a string. Types that are not specified (by their corresponding keys) are handled by the default formatters. Individual types for which a formatter can be set are: ... Total number of array elements which trigger summarization rather than full repr.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ numpy-array-strings
NumPy Array of Strings
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-ways-to-convert-a-string-list-to-a-numpy-array
5 Ways to Convert a String List to a NumPy Array โ€“ Be on the Right Side of Change
You can convert a list of strings to a numpy array of floats using the numpy.array() function along with the astype() method.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ user โ€บ basics.strings.html
Working with Arrays of Strings And Bytes โ€” NumPy v2.2 Manual
Working with data loaded or memory-mapped from a data file, where one or more of the fields in the data is a string or bytestring, and the maximum length of the field is known ahead of time. This often is used for a name or label field. Using NumPy indexing and broadcasting with arrays of Python strings of unknown length, which may or may not have data defined for every value.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.array2string.html
numpy.array2string โ€” NumPy v2.2 Manual
Callables should return a string. Types that are not specified (by their corresponding keys) are handled by the default formatters. Individual types for which a formatter can be set are: ... Total number of array elements which trigger summarization rather than full repr.
๐ŸŒ
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 output follows. OS is Win10-64 Pro, Python version...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.array2string.html
numpy.array2string โ€” NumPy v2.1 Manual
Callables should return a string. Types that are not specified (by their corresponding keys) are handled by the default formatters. Individual types for which a formatter can be set are: ... Total number of array elements which trigger summarization rather than full repr.