Convert NumPy Array to String in Python (6 Easy Methods)

When working with NumPy arrays in Python, you’ll often need to convert them to strings for display, logging, or storing in text formats. After years of working with NumPy, I’ve found several reliable approaches to transform arrays into readable string formats.

In this article, I’ll demonstrate six practical methods for converting NumPy arrays to strings in Python, along with examples that demonstrate when to use each technique.

Convert NumPy Array to String in Python

Now, I will explain to you some methods to convert a NumPy array to a string in Python.

Read NumPy Reverse Array in Python

Method 1: Use the str() Function

The simplest way to convert a NumPy array to a string is to use Python’s built-in str() function.

import numpy as np

# Create a sample NumPy array
arr = np.array([10, 20, 30, 40, 50])

# Convert to string using str()
string_arr = str(arr)

print(string_arr)

Output:

[10 20 30 40 50]

I executed the above example code and added the screenshot below.

numpy to string

This method is simple but gives you less control over formatting.

Check out NumPy Array to List in Python

Method 2: Use numpy.array2string() Function

The np.array2string() function offers more control over how your array is converted to a string in Python.

import numpy as np

# Create a sample NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Convert to string with custom formatting
string_arr = np.array2string(arr, separator=', ')

print(string_arr)

Output:

[[1, 2, 3],
 [4, 5, 6]]

I executed the above example code and added the screenshot below.

array to string python

This method is ideal when you need to customize the appearance of your string representation.

Read np.savetxt() Function in Python

Method 3: Use numpy.array_str() Function

Python np.array_str() function provides a simpler alternative with fewer options than array2string().

import numpy as np

# Create a NumPy array
arr = np.array([3.14159, 2.71828, 1.41421])

# Convert to string using array_str()
string_arr = np.array_str(arr, precision=3)

print(string_arr)

Output:

[3.142 2.718 1.414]

I executed the above example code and added the screenshot below.

python array to string

I find this method useful for quick conversions when I need basic formatting like precision control.

Check out np.genfromtxt() Function in Python

Method 4: Convert Array Elements Individually

For more granular control, you can convert individual elements to Python strings.

import numpy as np

# Create a NumPy array
arr = np.array([100, 200, 300, 400])

# Convert elements to strings and join them
string_elements = [str(element) for element in arr]
final_string = ', '.join(string_elements)

print(final_string)
# Output: "100, 200, 300, 400"

This approach is perfect when you want complete control over the final string format.

Read NumPy Reset Index of an Array in Python

Method 5: Use numpy.savetxt() with StringIO

For complex formatting, especially with large arrays, you can use np.savetxt() with a StringIO object.

import numpy as np
from io import StringIO

# Create a NumPy array
arr = np.array([[7.5, 8.1, 9.3], 
                [4.2, 5.7, 6.9]])

# Use StringIO as the output buffer
output = StringIO()
np.savetxt(output, arr, fmt='%.2f', delimiter=',')

# Get the string from the StringIO object
string_arr = output.getvalue()

print(string_arr)
# Output: "7.50,8.10,9.30
#          4.20,5.70,6.90"

I’ve found this method extremely useful for CSV-like formatting of arrays.

Check out Create a Python Empty Matrix

Method 6: Use pandas DataFrame

Converting through pandas provides excellent formatting options when working with data analysis projects.

import numpy as np
import pandas as pd

# Create a NumPy array representing sales data
sales_data = np.array([[1024.75, 897.50], 
                        [1542.33, 1173.25]])

# Convert to pandas DataFrame for better formatting
df = pd.DataFrame(sales_data, 
                  index=['Q1', 'Q2'], 
                  columns=['East Coast', 'West Coast'])

# Convert DataFrame to string
string_arr = df.to_string()

print(string_arr)
# Output:       East Coast  West Coast
#         Q1     1024.75      897.50
#         Q2     1542.33     1173.25

This method is ideal for tabular data where preserving row and column labels is important.

Real-World Example: Sales Analysis Report

Let’s look at a practical example where I needed to convert an array to a string for a sales report:

import numpy as np

# Monthly sales figures for a US retail chain ($K)
monthly_sales = np.array([
    [142.5, 138.7, 165.2],  # NYC
    [98.3, 112.5, 127.8],   # LA
    [87.2, 103.4, 115.9]    # Chicago
])

# City and month labels
cities = ['NYC', 'LA', 'Chicago']
months = ['January', 'February', 'March']

# Create a formatted report string
report = "Quarterly Sales Report (in $K):\n\n"
report += " " * 10 + " ".join([f"{m:^10}" for m in months]) + "\n"

for i, city in enumerate(cities):
    row = f"{city:10}"
    for sales in monthly_sales[i]:
        row += f"${sales:9.1f} "
    report += row + "\n"

print(report)
"""
Output:
Quarterly Sales Report (in $K):

           January   February    March   
NYC       $    142.5 $    138.7 $    165.2 
LA        $     98.3 $    112.5 $    127.8 
Chicago   $     87.2 $    103.4 $    115.9 
"""

In this example, I’m creating a nicely formatted sales report from a NumPy array, which could be easily included in an email or saved to a text file.

Read Random Number Between Two Values in Numpy

Which Method Should You Use?

After working with Python NumPy for over a decade, here’s my advice on choosing the right method:

  • Use str() for quick, informal string conversion
  • Use np.array2string() when you need formatting control
  • Use np.array_str() for simple numeric formatting
  • Use element-wise conversion for complete customization
  • Use np.savetxt() with StringIO for CSV-like output
  • Use pandas for tabular data with row/column labels

The right method depends on your specific use case, but having these six techniques in your toolbox covers almost every scenario you’ll encounter.

I hope you found this article helpful. I have explained six methods to convert a NumPy array to a string in Python, such as using the str() function, numpy.array2string() function, numpy.array_str() function, convert array elements individually, and using pandas dataframe.

Other Python articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.