Updated June 3, 2023
Introduction to NumPy Format
The format() function in Python converts a given value into a desired format as specified by the format specifier. It takes two parameters: value and format_specifier. The value parameter represents the value that needs to be converted, while the format_specifier parameter specifies the desired format for the conversion. 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 the value can be in any format.
Syntax:
The syntax forNumPyformat function in Python is 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
The working of the format function in NumPy is as follows:
- In Python, if you need to convert a given value into a specific format according to a format specifier, you can use the built-in format() function.
- 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 the value can be in any format.
Examples of NumPy Format
Following are the examples given below:
Example #1
Python program to demonstrate NumPyformat function to 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:
Example #2
Python program to demonstrate NumPy format function to convert a given value into the hexadecimal format of the 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:
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:
Recommended Articles
This is a guide to NumPy Format. Here we also discuss the introduction and working in numpy, along with different examples and its code implementation. You may also have a look at the following articles to learn more –