The conversion from Python String to Numpy array completely depends on what type of data your input string represents and what type of numpy array you want to create. For example, a string like “11 18 19 21” can be split and converted into an array of integers or floats.
However, depending on your current requirement, you can use the open of the below approaches:
- Using numpy.fromstring() (Most efficient and faster way)
- Using string.split() and numpy.array()
- String Representing a Matrix (2D Array)
- Handling Multi-Column Data (CSV-like Strings)
- String with Mixed Data Types
Before starting our tutorial, you must ensure the “numpy” library is installed on your system. If not, you can install it using the command below:
pip install numpy # OR python3 pip install numpy
Now, you can import it safely without any errors.
Approach 1: Using numpy.fromstring()
If your input contains raw numeric data (e.g., “1,2,3,4” or “1 2 3 4”), you can directly use the numpy.fromstring() with explicit delimiters and pass the string to it. This approach doesn’t require converting strings to lists in any step, so it is quite a faster and more efficient method.
import numpy as np string = "11 18 19 21 29" print(string) # Output: 11 18 19 21 29 print(type(string)) # Output: <class 'str'> array = np.fromstring(string, dtype=int, sep=' ') print(array) # Output: [11 18 19 21 29] print(type(array)) # Output: <class 'numpy.ndarray'>
In the above code, we used the type() function to check the data type of variables.
For comma-separated data
import numpy as np string = "11,18,19,21,29" print(string) # Output: 11,18,19,21,29 print(type(string)) # Output: <class 'str'> array = np.fromstring(string, dtype=int, sep=",") print(array) # Output: [11 18 19 21 29] print(type(array)) # Output: <class 'numpy.ndarray'>
Performance Tip: Pre-Allocation
For large strings, avoid intermediate lists. You can directly use the np.fromstring() method for conversion:
import numpy as np string = " ".join(map(str, range(10_000))) print(string) # Output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33... print(type(string)) # Output: <class 'str'> array = np.fromstring(string, dtype=int, sep=' ') print(array) # Output: [ 0 1 2 ... 9997 9998 9999] print(type(array)) # Output: <class 'numpy.ndarray'>
Approach 2: Using string.split() and numpy.array()
If you have a basic string and want to convert it into an array, first convert it into a list using the .split() function and then pass that list to the np.array() function.
import numpy as np string = "India Vs Pakistan CT 2025" print(string) # Output: India Vs Pakistan CT 2025 print(type(string)) # Output: <class 'str'> list = string.split(" ") print(list) # Output: ['India', 'Vs', 'Pakistan', 'CT', '2025'] array = np.array(list) print(type(array)) # Output: <class 'numpy.ndarray'>
For comma-separated values
If your input string has comma-separated values, you can pass the “,” as a separator to the .split() function. It will split that string by a comma and return the list. Then, pass that list to the np.array() function and pass the list, and dtype = float argument.
We want our output numpy array to contain float values instead of integer values.
import numpy as np string = "1,2,3,4,5" print(string) # Output: 1,2,3,4,5 print(type(string)) # Output: <class 'str'> list = list(string.split(",")) array = np.array(list, dtype=float) print(array) # Output: [1. 2. 3. 4. 5.] print(type(array)) # Output: <class 'numpy.ndarray'>
Passing “dtype” argument
If you want your output numpy array to contain integer values, you should pass dtype = np.int64 to the numpy.array() function as a second argument.
import numpy as np string = "1,2,3,4,5" print(string) # Output: 1,2,3,4,5 print(type(string)) # Output: <class 'str'> list = list(string.split(",")) array = np.array(list, dtype=np.int64) print(array) # Output: [1 2 3 4 5] print(type(array)) # Output: <class 'numpy.ndarray'>
Empty string
What if your string is empty? How will you convert that? An empty string ultimately creates an empty numpy array because of no elements.
Since an empty string contains nothing, you cannot use the .split() function to convert a string into a list. You can directly use the “.list()” function to convert it into a list and then convert that list into a numpy array.
import numpy as np string = "" print(string) # Output: "" print(type(string)) # Output: <class 'str'> list = list(string) array = np.array(list, dtype=np.int64) print(array) # Output: [] print(type(array)) # Output: <class 'numpy.ndarray'>
Approach 3: String Representing a Matrix (2D Array)
Sometimes, you come across a scenario where you find a string that represents a 2D array (e.g., “1 2; 3 4” or “[[1,2], [3,4]]”).
You can parse the string into a nested list first (using ast.literal_eval for safety), then convert it to a NumPy array.
import numpy as np import ast string = "[[1, 2], [3, 4]]" print(string) # Output: [[1, 2], [3, 4]] print(type(string)) # Output: <class 'str'> nested_list = ast.literal_eval(string) array = np.array(nested_list) print(array) # Output: [[1 2] # [3 4]] print(type(array)) # Output: <class 'numpy.ndarray'>
For space/semicolon-separated matrice
import numpy as np import ast string = "10 21; 18 19" print(string) # Output: 10 21; 18 19 print(type(string)) # Output: <class 'str'> rows = string.split(';') array = np.array([row.split() for row in rows], dtype=np.int64) print(array) # Output: [[10 21] # [18 19]] print(type(array)) # Output: <class 'numpy.ndarray'>
Approach 4: Handling Multi-Column Data (CSV-like Strings)
If your string has mult-column data with rows and columns (e.g., “1 2 3\n4 5 6”), you can split the string by lines and spaces using the .split(“\n”) method, use list comprehension, and then convert it to numpy array.
import numpy as np string = "1 2 3\n4 5 6" print(string) # Output: 1 2 3 # 4 5 6 print(type(string)) # Output: <class 'str'> rows = string.split('\n') array = np.array([row.split() for row in rows], dtype=np.int64) print(array) # Output: [[1 2 3] # [4 5 6]] print(type(array)) # Output: <class 'numpy.ndarray'>
Approach 5: Strings with Mixed Data Types
What if your input string contains mixed data types? In this case, you can extract data using the regex .findall() method and structure it into a NumPy array.
import numpy as np import re string = "apple 5, banana 3" print(string) # Output: apple 5, banana 3 print(type(string)) # Output: <class 'str'> data = re.findall(r'(\w+)\s+(\d+)', string) array = np.array(data, dtype='object') print(array) # Output: [['apple' '5'] # ['banana' '3']] print(type(array)) # Output: <class 'numpy.ndarray'>
That’s all!