BetterDocs
Home
Docs

Creation | np.fromstring()

Method:

np.fromstring(string, dtype=np.float64, count=-1, *, sep, like=None)

Interprets a string as an array.

Returns:

np.ndarray

Parameters:

string: str-

Input string.

import numpy as np

string = "1 2 3 4 5"
arr_from_string = np.fromstring(string=string, sep=' ')
print(arr_from_string) # Output: [1. 2. 3. 4. 5.]

dtype: data-type-

It will convert the elements to the specified data-type.

import numpy as np

string = "1 2 3 4 5"
arr_from_string = np.fromstring(string=string,dtype=np.int8, sep=' ')
print(arr_from_string) # Output: [1 2 3 4 5]

Values: +

count: int, Optional-

Number of items to read.

import numpy as np

string = "1 2 3 4 5"
arr_from_string = np.fromstring(string=string, dtype=np.int8, count=3, sep=' ')
print(arr_from_string) # Output: [1 2 3]

sep: str-

Separator between numbers.

import numpy as np

string = "1*2*3*4*5"
arr_from_string = np.fromstring(string=string, dtype=np.int8, sep='*')
print(arr_from_string) # Output: [1 2 3 4 5]

like: None, Optional-

The like parameter in np.fromstring() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy