A summary of the use of the print function in Python3

Source: Internet
Author: User
This article mainly summarizes the use of the print function in Python 3, the print function in Python3 and the usage of the previous version are very different, this article through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, The friends who need to take a look below.

Objective

Python thought: "Everything is an Object!" "Recently found that there are many differences in the use of print in Python3 and Python2, the parentheses are required in Python3, and the indentation is to use 4 spaces (which is not required, but you'd better), and the indentation represents the beginning of a block of code, and non-indentation represents the end of a code. There are no definite curly braces, brackets, or keywords. This means that the gap is important and must be consistent. The first line without indentation marks the end of a code block, meaning a function, an if statement, a for loop, while loop, and so on. So I want to give you a summary of the Python3 in the Print function usage related content, words not much, come together to see the detailed introduction:

1. Output strings and Numbers


>>> print ("Runoob") # output string Runoob >>> print (+)   # output Digital 100>>> str = ' Runoob ' >>> Print (str)   # output variable runoob>>> L = [N, ' a ']   # list >>> print (L) [1, 2, ' a '] >>> t = ("a"   # tuples >>> print (t) (1, 2, ' a ') >>> d = {' A ': 1, ' B ': 2} # Dictionary >>> print (d) {' A ': 1, ' B ': 2}

2. Format the output integer


<p> supports parameter formatting, similar to the C-language printf </p><pre>>>> str = "The length of (%s) is%d"% (' Runoob ', Len (' Runoo B ')) >>> print (str) the length of (Runoob) is 6

Python string formatting symbols:

symbols Description
%c Formatting characters and their ASCII code
%s formatting strings
%d formatting integers
%u Formatting an unsigned integer
%o Formatting an unsigned octal number
%x formatting unsigned hexadecimal numbers
%x Format unsigned hexadecimal number (uppercase)
%f Format floating-point numbers to specify the precision after a decimal point
%e Format floating-point numbers with scientific notation
%E function with%e, format floating-point numbers with scientific notation
%g Shorthand for%f and%e
%G Shorthand for%f and%E
%p Format the address of a variable with hexadecimal number

Formatting operator Auxiliary directives:

symbols function
* Define width or decimal precision
- Used for left justification
+ Show plus sign (+) in front of positive number
<sp> Show spaces in front of positive numbers
# Displays 0 (' 0 ') before the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on ' x ' or ' x ')
0 The displayed number is preceded by ' 0 ' instead of the default space
% ' percent ' output a single '% '
(VAR) mapping variables (dictionary parameters)
M.N. M is the minimum total width displayed, and n is the number of digits after the decimal point (if available)

3. Formatted output 16 binary, decimal, octal integer

    • #%x---hex hex

    • #%d---Dec decimal

    • #%o---Oct octal


>>> Nhex = 0xff>>> print ("Nhex =%x,ndec =%d,noct =%o"% (nhex,nhex,nhex)) Nhex = Ff,ndec = 255,noct = 3 77

4. Formatted output floating-point number (float)


>>> pi = 3.141592653 >>> print ('%10.3f '% pi) #字段宽10, accuracy 3   3.142 >>> print ("PI =%.*f"% (3,p i)) #用 * Read field width or precision pi = 3.142 >>> print ('%010.3f '% pi) from a subsequent tuple #用0填充空白 000003.142 >>> print ('%-10.3f '% pi ) #左对齐 3.142  >>> print ('%+f '% pi) #显示正负号 +3.141593

5. Automatic line Wrapping

Print automatically adds a carriage return at the end of the line, and if you don't need a carriage return, simply add a comma at the end of the print statement to change its behavior.


>>> for I in Range (0,6): ...  Print (I,) ... 012345

6. Print No Line break

In Python, print is wrapped by default.


>>> for I in Range (0,3): ...  Print (i) ... 012>>>

To change lines you should write print (I, end = ")


>>> for I in Range (0,3): ...  Print (I, end = ") ... 012

Summarize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.