Open In App

numpy.fromstring() function – Python

Last Updated : 18 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

numpy.fromstring() function create a new one-dimensional array initialized from text data in a string.

Syntax : numpy.fromstring(string, dtype = float, count = -1, sep = ‘ ‘)

Parameters :

string : [str] A string that contained the data.

dtype : [data-type, optional] Data-type of the array. Default data type is float.

count : [int, optional] Number of items to read. If this is negative (the default), the count will be determined from the length of the data.

sep : [str, optional] The string separating numbers in the data.

Return : [ndarray] Return the constructed array.

Code #1 :

Python3




# Python program explaining
# numpy.fromstring() function
   
# importing numpy as geek
import numpy as geek
   
gfg = geek.fromstring('1 2 3 4 5', dtype = float, sep = ' ')
   
print(gfg)


Output :

[1. 2. 3. 4. 5.]

Code #2 :

Python3




# Python program explaining
# numpy.fromstring() function
   
# importing numpy as geek
import numpy as geek
   
gfg = geek.fromstring('1 2 3 4 5 6', dtype = int, sep = ' ')
   
print(gfg)


Output :

[1 2 3 4 5 6]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads