Home  >  Article  >  Backend Development  >  Summary of the use of print function in Python3

Summary of the use of print function in Python3

黄舟
黄舟Original
2017-08-08 11:34:082394browse

This article mainly gives you a summary of how to use the print function in Python 3. The print function in python3 is very different from the usage in previous versions. This article introduces it in detail through sample code, which is very useful for everyone's study or work. It has certain reference and learning value. Friends who need it can take a look below.

Preface

Python Thought: "Everything is an object!", I recently discovered that there are many differences in the usage of print in python3 and python2, python3 You need to use parentheses in the text, and use 4 spaces for indentation (this is not required, but you'd better do it). Indentation indicates the beginning of a code block, and non-indentation indicates the end of a code block. There are no explicit braces, brackets, or keywords. This means that whitespace is important and must be consistent. The first unindented line marks the block of code, meaning the end of a function, if statement, for loop, while loop, etc. So I want to summarize for you the usage of the print function in Python3. I don’t have much to say. Let’s take a look at the detailed introduction:

1. Output string and numbers


##

>>> print("runoob") # 输出字符串
runoob 
>>> print(100)   # 输出数字
100
>>> str = 'runoob'
>>> print(str)   # 输出变量
runoob
>>> L = [1,2,'a']   # 列表 
>>> print(L) 
[1, 2, 'a'] 
>>> t = (1,2,'a')   # 元组
>>> print(t) 
(1, 2, 'a') 
>>> d = {'a':1, 'b':2} # 字典
>>> print(d) 
{'a': 1, 'b': 2}

2. Formatted output integers


<p>支持参数格式化,与 C 语言的 printf 类似</p>

<pre class="brush:php;toolbar:false">>>> str = "the length of (%s) is %d" %(&#39;runoob&#39;,len(&#39;runoob&#39;))
>>> print(str)
the length of (runoob) is 6

python string formatting symbols:

## symbol number %c## %s Format string %d Format integer %u Format unsigned Integer %o Format unsigned octal number %x Format none Signed hexadecimal number %X Format unsigned hexadecimal number (uppercase) % f Format floating point numbers, you can specify the precision after the decimal point %e Format floating point numbers using scientific notation %E The function is the same as %e, using scientific notation to format floating point numbers %g %f and % The abbreviation for e %G The abbreviation for %f and %E %p Use Address of hexadecimal format variableFormat operator auxiliary command:
Description
Formatting characters and their ASCII codes


SymbolFunction*Define width or decimal point precision -Used for left alignment+Display plus sign (+) in front of positive numbers40248cdb8ba3b8fe238e2ffa5e6e3cd8Display spaces before positive numbersDisplay zero ('0') before octal numbers, in '0x' or '0X' is displayed in front of the hexadecimal number (depending on whether 'x' or 'X' is used) 0 is displayed in front of the number Pad '0' instead of default spaces% '%%' outputs a single '%'(var)Mapping variable (dictionary parameter)m.n.m is the minimum total width displayed, n is the number of digits after the decimal point (If available)##3. Formatted output of hexadecimal, decimal, octal integers


#%x --- hex hexadecimal

  • #%d --- dec decimal

  • #%o --- oct octal

  • >>> nHex = 0xFF
    >>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
    nHex = ff,nDec = 255,nOct = 377


4. Formatted output floating point number (float)


>>> pi = 3.141592653 
>>> print(&#39;%10.3f&#39; % pi) #字段宽10,精度3 
  3.142 
>>> print("pi = %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度 
pi = 3.142 
>>> print(&#39;%010.3f&#39; % pi) #用0填充空白 
000003.142 
>>> print(&#39;%-10.3f&#39; % pi) #左对齐 
3.142  
>>> print(&#39;%+f&#39; % pi) #显示正负号 
+3.141593


5. Automatic line wrapping

print A carriage return will be automatically added at the end of the line. If a carriage return is not required, just add a comma at the end of the print statement to change its behavior.


>>> for i in range(0,6):
...  print (i,)
... 
0
1
2
3
4
5


6. Print without line breaks

Default print in Python It’s a line break


>>> for i in range(0,3):
...  print (i)
... 
0
1
2
>>>

If you want a line break you should write print(i, end = '' )



>>> for i in range(0,3):
...  print(i, end = &#39;&#39; )
... 
012


Summary

The above is the detailed content of Summary of the use of print function in Python3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn