Introduction to NumPy Format
If the given value must be converted into a required format as specified by the format specifier, then we make use of a built in function called format() function in python. It takes two parameters value and formatspecifier where value is the value that is to be converted into a format as specified by the second parameter formatspecifier and there are several formatspecifiers such as <, >, ^, =, +, -, _, comma, space, b, c, d, e, E, f, F, g, G, o, x, X, n and % and the first parameter specified as value can be in any format.
Syntax:
The syntax forNumPyformat functionin Pythonis as follows:
format(value, formatspecifier)
where value is the value that is to be converted into a format as specified by the second parameter formatspecifier.
Working of Format Function in NumPy
Working of format function in NumPy is as follows:
- If the given value must be converted into a required format as specified by the format specifier, then we make use of a built in function called format() function in python.
- The format() function in python takes two parameters value and formatspecifier where value is the value that is to be converted into a format as specified by the second parameter formatspecifier.
- There are several formatspecifiers such as <, >, ^, =, +, -, _, comma, space, b, c, d, e, E, f, F, g, G, o, x, X, n and % and the first parameter specified as value can be in any format.
Examples of NumPy Format
Following are the examples as given below:
Example #1
Python program to demonstrate NumPyformat functionto convert a given value into binary format:
Code:
#the value to be converted to a binary format as specified by the format specifier is displayed on the screen
value = 10
print('The given value that is to be converted to a binary value in the format specified is')
print value
print '\n'
#format function is used to convert the given value into a format as specified by the format specifier
convertedvalue = format(value, 'b')
print('The given value that is converted to a binary value in the format specified is')
#the given value after converted to a binary format as specified by the format specifier is displayed on the screen as the output
print convertedvalue
print '\n'
Output:
In the above program, the value to be converted to a binary format as specified by the format specifier is stored in a variable called value and is displayed on the screen. Then the format function is used to convert the given value into a binary format as specified by the format specifier. Then the given value after converted to a binary format as specified by the format specifier is displayed on the screen as the output.
Example #2
Python program to demonstrate NumPy format function to convert a given value into hexadecimal format of lower case:
Code:
#the value to be converted to ahexa decimal format of lower case as specified by the format specifier is displayed on the screen
value = 1000
print('The given value that is to be converted to a hexa decimal format of lower case value in the format specified is')
print value
print '\n'
#format function is used to convert the given value into ahexa decimal format of lower caseas specified by the format specifier
convertedvalue = format(value, 'x')
print('The given value that is converted to a hexa decimal format of lower case value in the format specified is')
#the given value after converted to ahexa decimal format of lower case as specified by the format specifier is displayed on the screen as the output
print convertedvalue
print '\n'
Output:
In the above program, the value to be converted to a hexa decimal format of lower case as specified by the format specifier is stored in a variable called value and is displayed on the screen. Then the format function is used to convert the given value into a hexa decimal format of lower case as specified by the format specifier. Then the given value after converted to ahexa decimal format of lower case as specified by the format specifier is displayed on the screen as the output.
Example #3
Python program to demonstrate NumPy format function to convert a given value into octal format:
Code:
#the value to be converted to a octal format as specified by the format specifier is displayed on the screen
value = 100
print('The given value that is to be converted to a octal value in the format specified is')
print value
print '\n'
#format function is used to convert the given value into a octal format as specified by the format specifier
convertedvalue = format(value, 'o')
print('The given value that is converted to a octal value in the format specified is')
#the given value after converted to aoctal format as specified by the format specifier is displayed on the screen as the output
print convertedvalue
print '\n'
Output:
In the above program, the value to be converted to an octal format as specified by the format specifier is stored in a variable called value and is displayed on the screen. Then the format function is used to convert the given value into an octal format as specified by the format specifier. Then the given value after converted to an octal format as specified by the format specifier is displayed on the screen as the output.
Recommended Articles
This is a guide to NumPy Format. Here we also discuss the introduction and working of format function in numpy along with different examples and its code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses