numpy.fromstring() function – Python (original) (raw)
Last Updated : 18 Aug, 2020
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
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
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]
Similar Reads
- numpy.fromiter() function – Python NumPy's fromiter() function is a handy tool for creating a NumPy array from an iterable object. This iterable can be any Python object that provides elements one at a time. The function is especially useful when you need to convert data from a custom data source, like a file or generator, into a Num 2 min read
- numpy.find() in Python numpy.core.defchararray.find(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An integer array wi 1 min read
- Python 3 - input() function In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string. Python input() Function SyntaxSyntax: input(prompt) Parameter: Prompt: (optio 3 min read
- numpy.index() in Python numpy.core.defchararray.index(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range But if substring is not found, it raises ValueError. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, option 1 min read
- numpy.typename() function – Python numpy.typename() function return a description for the given data type code. Syntax : numpy.typename(char) Parameters : char : [str] Data type code. Return : [str] Description of the input data type code. Code #1 : # Python program explaining # numpy.typename() function # importing numpy as geek imp 2 min read
- Python len() Function The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example: [GFGTABS] Python s = "G 2 min read
- Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part. Example: In this example, we passed a string as an argument to the int() function and printed it. [GFGTABS] Python age = "21" 4 min read
- numpy string operations | find() function numpy.core.defchararray.find(arr, sub, start=0, end=None) is another function for doing string operations in numpy.It returns the lowest index in the string where substring sub is found for each element in arr within the range start to end.It returns -1 otherwise. Parameters: arr : array_like of str 2 min read
- numpy.sctype2char() function – Python numpy.sctype2char() function return the string representation of a scalar dtype. Syntax : numpy.sctype2char(sctype) Parameters : sctype : [scalar dtype or object] If a sctype is a scalar dtype, the corresponding string character is returned. If an object, sctype2char tries to infer its scalar type a 1 min read
- Python oct() Function Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal 2 min read