Summary of using the print function in Python 3, pythonprint

Source: Internet
Author: User

Summary of using the print function in Python 3, pythonprint

Preface

Python: "Everything is an object !", Recently, we found that the usage of print in python3 and python2 is many different. In python3, parentheses are used and four spaces are used for indentation (this is not necessary, but you 'd better do this ), indentation indicates the beginning of a code block, and non-indent indicates the end of a code. There are no explicit braces, braces, or keywords. This means that the blank space is important and must be consistent. The first line without indentation marks a code block, which means the end of a function, if statement, for loop, while loop, and so on. So I want to summarize the usage of the print function in Python3. Let's take a look at the details:

1. Output string and number

>>> Print ("runoob") # output string runoob >>> print (100) # output number 100 >>>> str = 'runoob' >>> print (str) # output variable runoob >>> L = [1, 2, 'a'] # list >>> print (L) [1, 2, 'A'] >>> t = (1, 2, '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 printf in C Language </p> <pre >>>> str = "the length of (% s) is % d "% ('runoob', len ('runoob') >>> print (str) the length of (runoob) is 6

Python string formatting symbol:

Letter Number Description
% C Format characters and their ASCII codes
% S Format a string
% D Format an integer
% U Format an unsigned integer
% O Format the unsigned octal number
% X Format the unsigned hexadecimal number
% X Format the unsigned hexadecimal number (uppercase)
% F Format a floating point number to specify the decimal point precision.
% E Formatting floating-point numbers using scientific notation
% E The function is the same as % e. The floating point is formatted using scientific notation.
% G Abbreviation of % f and % e
% G Abbreviation of % f and % E
% P Format the address of a variable in hexadecimal notation

Auxiliary instructions for formatting operators:

Symbol Function
* Define the width or decimal point Precision
- Align left
+ Displays the plus sign (+) before a positive number)
<Sp> A space is displayed before a positive number.
# Display zero ('0') before the octal number and '0x 'or '0x' before the hexadecimal number (depending on whether 'X' or 'X' is used ')
0 Fill in '0' before the displayed number, instead of the default space.
% '%' Outputs a single '%'
(Var) Ing variables (Dictionary parameters)
M. n. M is the minimum total width of the display, and n is the number of digits after the decimal point (if available)

3. formatted and output hexadecimal, decimal, and octal integers

  • # % X --- hex hexadecimal
  • # % D --- decimal
  • # % O --- oct octal
>>> nHex = 0xFF>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))nHex = ff,nDec = 255,nOct = 377

4. format the output floating point number (float)

>>> Pi = 3.141592653 >>> print ('% 10.3f' % pi) # field width 10, precision 3 3.142 >>> print ("pi = %. * f "% (3, pi) # Use * to read the field width or precision from the following tuples. pi = 3.142 >>>> print ('% 010.3f' % pi) # Fill the blank with 0: 000003.142 >>> print ('%-10.3f' % pi) # align left: 3.142 >>>> print (' % + F' % pi) # Show plus and minus signs + 3.141593

5. Automatic line feed

Print automatically adds a carriage return at the end of the row. If you do not need to press enter, you only need to 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 does not wrap

In Python, print is a line feed by default.

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

To wrap a line, you should write it as print (I, end = '')

>>> for i in range(0,3):...  print(i, end = '' )... 012

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.