Simply use str():

import numpy as np

x = np.array([1, 2, 3, 4])

print(type(x))

print(type(str(x)))

Yields:

<class 'numpy.ndarray'>
<class 'str'>
Answer from rahlf23 on Stack Overflow
Discussions

Convert numpy, list or float to string in python - Stack Overflow
-1 How do you convert a numpy.float64 to Python native string type for writing to a file with write()? More on stackoverflow.com
🌐 stackoverflow.com
Can't convert object of type 'numpy.float64' to 'str' for 'filename'
Hello, have a nice day!! When to try implement the code; def has_mask(mask_path): img = cv2.imread(mask_path, 0) thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)[1] if np.mean(thresh) > 0.0: return True else: return False def get_dataloader(rank, world_size): “”“Creates the data ... More on discuss.pytorch.org
🌐 discuss.pytorch.org
1
0
December 21, 2021
python - Printing numpy.float64 with full precision - Stack Overflow
What is the proper/accepted way to print and convert a numpy.float64 to a string? I've noticed just using print or str() will lose some precision. However, repr maintains the full precision. For More on stackoverflow.com
🌐 stackoverflow.com
ENH: Numpy arrays of floats converted to string using astype(str) have unexpected behaviour
Proposed new feature or change: When converting an array of very small floats to str, the astype() function occasionally returns scientific notation instead of a str representation of a float. This feels like an uninteded issue with the ... More on github.com
🌐 github.com
February 17, 2024
People also ask

How do I convert a float to a string?
Use str(value) for the default representation or a format specification when the output needs a defined precision.
🌐
pythonpool.com
pythonpool.com › home › tutorials › convert a python float to a string: precision and formatting
Convert a Python Float to a String: Precision and Formatting
Does formatting change the float?
No. Formatting creates text; it does not change the original numeric value unless you replace it with the string.
🌐
pythonpool.com
pythonpool.com › home › tutorials › convert a python float to a string: precision and formatting
Convert a Python Float to a String: Precision and Formatting
When should I use scientific notation?
Use scientific notation for very large or very small values when the exponent is clearer than a long sequence of zeros.
🌐
pythonpool.com
pythonpool.com › home › tutorials › convert a python float to a string: precision and formatting
Convert a Python Float to a String: Precision and Formatting
🌐
AskPython
askpython.com › python › string › python-string-float-conversion
Python String to float, float to String - AskPython
February 28, 2020 - List Comprehension can be used to convert Python NumPy float array to an array of String elements.
🌐
IONOS
ionos.com › digital guide › websites › web development › converting python strings to floats
How to convert Python strings to floats - IONOS
February 1, 2025 - First you’ll need to import NumPy in your script if it’s not already installed. You can then use NumPy to apply the function numpy.float64() to the string string_value to convert it into a 64-bit floating number.
Find elsewhere
🌐
Python Pool
pythonpool.com › home › tutorials › convert a python float to a string: precision and formatting
Convert a Python Float to a String: Precision and Formatting
July 13, 2026 - Good tests should include whole-number floats, values with several decimals, negative values, very small values, and values that round up. These cases confirm that the string output matches the display rule you intended.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-of-float-to-string-conversion
Python - List of float to string conversion - GeeksforGeeks
July 12, 2025 - This method is useful when you want to output the list as a single formatted string. If the list is a numpy array, we can use numpy.vectorize() to perform the conversion efficiently.
🌐
NumPy
numpy.org › doc › 2.1 › user › basics.types.html
Data types — NumPy v2.1 Manual
For example, numpy.float64 is a 64 bit floating point data type. Some types, such as numpy.int_ and numpy.intp, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit CPU architectures). This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. In addition to numerical types, NumPy also supports storing unicode strings, via the numpy.str_ dtype (U character code), null-terminated byte sequences via numpy.bytes_ (S character code), and arbitrary byte sequences, via numpy.void (V character code).
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.fromstring.html
numpy.fromstring — NumPy v2.5 Manual
June 22, 2021 - 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.
🌐
Beautiful Soup
tedboy.github.io › numpy › generated › generated › numpy.float64.html
1.7.36. numpy.float64 — Numpy API
1.7.36. numpy.float64 · View page source · class numpy.float64¶ · 64-bit floating-point number.
🌐
GitHub
github.com › numpy › numpy › issues › 25841
ENH: Numpy arrays of floats converted to string using astype(str) have unexpected behaviour · Issue #25841 · numpy/numpy
February 17, 2024 - The only ways to convert the floats to the inteded string representation is to explicity declare it using a very slow and unvectorized for loop etc., or by using very strange or difficult methods like arr = np.vectorize(np.core.multiarray.d...
🌐
Mkyong
mkyong.com › home › python › python – how to convert float to string
Python – How to convert float to String | mkyong.com
December 14, 2018 - In Python, we can use str() to convert float to String · Was this article helpful
🌐
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.6.dev0 Manual
For example, numpy.float64 is a 64 bit floating point data type. Some types, such as numpy.int_ and numpy.intp, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit CPU architectures). This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. In addition to numerical types, NumPy also supports storing unicode strings, via the numpy.str_ dtype (U character code), null-padded byte sequences via numpy.bytes_ (S character code), and arbitrary byte sequences, via numpy.void (V character code).
🌐
NumPy
numpy.org › doc › 2.2 › user › basics.types.html
Data types — NumPy v2.2 Manual
For example, numpy.float64 is a 64 bit floating point data type. Some types, such as numpy.int_ and numpy.intp, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit CPU architectures). This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. In addition to numerical types, NumPy also supports storing unicode strings, via the numpy.str_ dtype (U character code), null-terminated byte sequences via numpy.bytes_ (S character code), and arbitrary byte sequences, via numpy.void (V character code).
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.fromstring.html
numpy.fromstring — NumPy v2.6.dev0 Manual
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.
Top answer
1 of 5
45

You seem a bit confused as to how numpy arrays work behind the scenes. Each item in an array must be the same size.

The string representation of a float doesn't work this way. For example, repr(1.3) yields '1.3', but repr(1.33) yields '1.3300000000000001'.

A accurate string representation of a floating point number produces a variable length string.

Because numpy arrays consist of elements that are all the same size, numpy requires you to specify the length of the strings within the array when you're using string arrays.

If you use x.astype('str'), it will always convert things to an array of strings of length 1.

For example, using x = np.array(1.344566), x.astype('str') yields '1'!

You need to be more explict and use the '|Sx' dtype syntax, where x is the length of the string for each element of the array.

For example, use x.astype('|S10') to convert the array to strings of length 10.

Even better, just avoid using numpy arrays of strings altogether. It's usually a bad idea, and there's no reason I can see from your description of your problem to use them in the first place...

2 of 5
25

If you have an array of numbers and you want an array of strings, you can write:

strings = ["%.2f" % number for number in numbers]

If your numbers are floats, the array would be an array with the same numbers as strings with two decimals.

>>> a = [1,2,3,4,5]
>>> min_a, max_a = min(a), max(a)
>>> a_normalized = [float(x-min_a)/(max_a-min_a) for x in a]
>>> a_normalized
[0.0, 0.25, 0.5, 0.75, 1.0]
>>> a_strings = ["%.2f" % x for x in a_normalized]
>>> a_strings
['0.00', '0.25', '0.50', '0.75', '1.00']

Notice that it also works with numpy arrays:

>>> a = numpy.array([0.0, 0.25, 0.75, 1.0])
>>> print ["%.2f" % x for x in a]
['0.00', '0.25', '0.50', '0.75', '1.00']

A similar methodology can be used if you have a multi-dimensional array:

new_array = numpy.array(["%.2f" % x for x in old_array.reshape(old_array.size)])
new_array = new_array.reshape(old_array.shape)

Example:

>>> x = numpy.array([[0,0.1,0.2],[0.3,0.4,0.5],[0.6, 0.7, 0.8]])
>>> y = numpy.array(["%.2f" % w for w in x.reshape(x.size)])
>>> y = y.reshape(x.shape)
>>> print y
[['0.00' '0.10' '0.20']
 ['0.30' '0.40' '0.50']
 ['0.60' '0.70' '0.80']]

If you check the Matplotlib example for the function you are using, you will notice they use a similar methodology: build empty matrix and fill it with strings built with the interpolation method. The relevant part of the referenced code is:

colortuple = ('y', 'b')
colors = np.empty(X.shape, dtype=str)
for y in range(ylen):
    for x in range(xlen):
        colors[x, y] = colortuple[(x + y) % len(colortuple)]

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
        linewidth=0, antialiased=False)
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.5 Manual
For example, numpy.float64 is a 64 bit floating point data type. Some types, such as numpy.int_ and numpy.intp, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit CPU architectures). This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. In addition to numerical types, NumPy also supports storing unicode strings, via the numpy.str_ dtype (U character code), null-terminated byte sequences via numpy.bytes_ (S character code), and arbitrary byte sequences, via numpy.void (V character code).