当前位置:网站首页>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/
边栏推荐
- Promethus
- P5.js development - setting
- Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
- Chapter 1: recursively find the factorial n of n!
- 6. Data agent object Defineproperty method
- Win10 share you don't have permission
- Exercises of function recursion
- 4. Data splitting of Flink real-time project
- Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
- Point cloud data denoising
猜你喜欢

01 - QT OpenGL display OpenGL window
![Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively](/img/c5/0081689817700770f6210d50ec4e1f.png)
Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively

Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue

PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?

第一章:求n的阶乘n!
![[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University](/img/95/5baf5c8bedb00e67394a6c0a8234ff.png)
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University

05 -- QT OpenGL draw cube uniform

Network security Kali penetration learning how to get started with web penetration how to scan based on nmap

Acquisition and transmission of parameters in automatic testing of JMeter interface

JMeter connection database
随机推荐
Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
第一章: 舍罕王失算
[effective Objective-C] - block and grand central distribution
Day10 ---- 强制登录, token刷新与jwt禁用
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
Xctf attack and defense world crypto master advanced area olddriver
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Class loading process
Realize user registration and login
Phpstudy set LAN access
第一章:求奇因数代数和,求同吗小数和s(d, n),简化同码小数和s(d, n),拓广同码小数和s(d, n)
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
第一章:拓广同码小数和s(d, n)
Bool blind note - score query
Change deepin to Alibaba image source
第一章:三位阶乘和数,图形点扫描
Explore the internal mechanism of modern browsers (I) (original translation)
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
Rd file name conflict when extending a S4 method of some other package
Nerfplusplus parameter format sorting