当前位置:网站首页>2.7 format output of values
2.7 format output of values
2022-07-03 20:00:00 【leeshuqing】
It mainly introduces the common format output of various data types .
1) Let's first look at the problem of integers , That is, the formatted display of different base numbers . For example, for decimal integers , Whether it can be displayed in other base numbers ?

Let's first look at the use print Function format function :
num = 100
print('%x' % num)
Output is : 64. there % The character represents the marker conversion specifier ,%x Said to 16 Hexadecimal display value , This is the old version Python Writing . We can use a more powerful new way of writing .
num = 100
print(format(num, 'b'))
print(format(num, 'o'))
print(format(num, 'x'))
print(format(num, 'd'))
Output is :
1100100
144
64
100
This shows that using format Function to realize format conversion , And it can also be converted to other base numbers , It's all through format The second parameter is to distinguish , such as b、o、x、d Respectively represent binary 、 octal 、 Hexadecimal and decimal .
format You can also convert other base numbers to decimal :
num = 0b100
print(format(num, 'd'))
Output is :4.
You can also use some special functions to complete the conversion to other specific base numbers , For example, the conversion from decimal to other decimal :
num = 100
print(bin(num))
print(oct(num))
print(hex(num))
Output is :
0b1100100
0o144
0x64
But there is no ready-made function that can be converted to decimal function , Therefore, we can skillfully use combination to realize :
num = 0b100
print(int(str(num), 10))
Output is :4.
2) Let's look at the problem of floating point numbers , This paper mainly introduces the format conversion of scientific counting .

For example, decimal conversion to scientific counting :
num = 1 / 3
print('{:.4e}'.format(num))
Output is :
3.3333e-01
among 4 It can be replaced as appropriate , there 4 Express 4 For decimal places . Whole {:.4e} It is an output format of scientific counting . It can also be written in another old version with the same effect :
num = 1 / 3
print('%.4e' % num)
The output content is the same as .
In turn, , Scientific counting method can also be converted to normal decimal form :
num = 3.3333e-01
print('{:.3f}'.format(num))
Output is :
0.333
Please pay attention to e Instead of f The difference between .
You can also use the old version :
num = 3.3333e-01
print('%.3f' % num)
The output content is the same as .
3) Finally, let's look at the formatted output of the string .

The formatted output content of string is the most diverse , It is also the most practical . Let's illustrate with a few examples . Let's first look at the output of formatted variable values . This is the most primitive method , Directly use the plus sign to connect the string to form the required output format and content :
strs = ' nanjing '
print(' The value of the variable is ' + strs)
Output is : The value of the variable is Nanjing
You can also use format Format :
strs = ' nanjing '
print(' The value of the variable is {}'.format(strs))
The output content is the same as .print Curly braces for parameters in function {} Represents a placeholder , It can be understood as an empty , wait for format Function parameter variable value to fill . And even other data types can :
num = 1
print(' The value of the variable is {}'.format(num))
Output is :
The value of the variable is 1
Obviously, if so + How to write it , You also need to convert data types , Therefore, this writing method is simpler .
For multiple variables , Still convenient :
strs1 = ' nanjing '
strs2 = ' jiangsu '
print('{} yes {} The provincial capital, '.format(strs1, strs2))
Output is :
Nanjing is the capital of Jiangsu Province
You can also write like this :
strs1 = ' nanjing '
strs2 = ' jiangsu '
print(f'{strs1} yes {strs2} The provincial capital, ')
The output content is the same as ,f The prefix indicates that what is in all curly braces is the corresponding variable to be output .
You can also use % Form placeholders achieve the same effect , Such as %s Representation string :
strs1 = ' nanjing '
strs2 = ' jiangsu '
print('%s yes %s The provincial capital, ' % (strs1, strs2))
The output content is the same as . We will use some later exercises format Function other format usage , At that time, we will learn and use .
Supporting learning resources 、 MOOC video :
Python Big data analysis - Shu Qing Li
https://www.njcie.com/python/
边栏推荐
- Use of aggregate functions
- BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
- 2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
- How to improve data security by renting servers in Hong Kong
- Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
- 原生表格-滚动-合并功能
- 10 smart contract developer tools that miss and lose
- How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
- JMeter connection database
- Pat grade B 1009 is ironic (20 points)
猜你喜欢

Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla

Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a

Commands related to files and directories

5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes

第一章:拓广同码小数和s(d, n)

Promethus

10 smart contract developer tools that miss and lose
![Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue](/img/5b/fc776a1982e24b82984d82be6a016f.jpg)
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue

第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组

HCIA-USG Security Policy
随机推荐
02 -- QT OpenGL drawing triangle
Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
The simplicity of laravel
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
Chapter 1: King Shehan miscalculated
Ae/pr/fcpx super visual effects plug-in package fxfactory
Unittest framework is basically used
Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
AcWing 1460. Where am i?
Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
Phpstudy set LAN access
Rad+xray vulnerability scanning tool
PR notes:
Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
2022-06-28 advanced network engineering (XIII) IS-IS route filtering, route summary, authentication, factors affecting the establishment of Isis neighbor relations, other commands and characteristics
Leetcode 1189. Maximum number of balloons (special character count)
The 15 year old interviewer will teach you four unique skills that you must pass the interview
原生表格-滚动-合并功能