A detailed description of Python's print function

Source: Internet
Author: User
Tags pow

The output print function summarizes:

1. String and numeric types
can be directly output

[Python]View PlainCopy
    1. >>> Print (1)
    2. 1
    3. >>> Print ("Hello World")
    4. Hello World


2. Variables
No matter what type, value, Boolean, List, dictionary ... can be directly output

[Python]View PlainCopy
  1. >>> x =
  2. >>> Print (x)
  3. 12
  4. >>> s = ' Hello '
  5. >>> Print (s)
  6. Hello
  7. >>> L = [1,2,' a ']
  8. >>> Print (L)
  9. [1, 2, ' a ']
  10. >>> t = (1,2,' a ')
  11. >>> print (t)
  12. (1, 2, ' a ')
  13. >>> d = {' a ':1, ' B ':2}
  14. >>> print (d)
  15. {' a ': 1, ' B ': 2}


3. Formatted output
Similar to printf in C

[Python]View PlainCopy
    1. >>> s
    2. ' Hello '
    3. >>> x = Len (s)
    4. >>> Print ("The length of%s is%d"% (s,x))
    5. The length of Hello is 5


Look at the summary of formatted output in Python BASIC programming:

(1).% character: The beginning of the token conversion specifier


(2). Convert flag:-Indicates left alignment, + indicates a positive sign before the value is converted, "" (white space character) to retain a white space before, and 0 for the conversion value if the number of digits is not enough. 0 padding


(3). Minimum field width: The converted string should have at least the width specified by the value. If it is *, the width is read from the value tuple.


(4). dot (.) Heel Precision Value: If the conversion is real, the precision value represents the number of digits after the decimal point. If the string is converted, the number represents the maximum field width. If it is *, then the precision will be read from the tuple

(5). string format conversion type


Conversion type meaning

D,i signed Decimal integer
o unsigned octal
U non-signed decimal
x hexadecimal without symbol (lowercase)
X hexadecimal without symbol (uppercase)
Floating point number represented by the e-scientific notation (lowercase)
The floating-point number represented by the E-scientific notation (uppercase)
F,f decimal Floating-point number
G If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
G if the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
C Single-character (accepts integers or single character strings)
R string (convert any Python object using repr)
s string (convert any Python object using str)

[Python]View PlainCopy
  1. >>> pi = 3.141592653
  2. >>> Print ('%10.3f '% pi) #字段宽10, accuracy 3
  3. 3.142
  4. >>> print ("PI =%.*f"% (3,pi)) #用 * Read field width or precision from subsequent tuples
  5. Pi = 3.142
  6. >>> Print ('%010.3f '% pi) #用0填充空白
  7. 000003.142
  8. >>> Print ('%-10.3f '% pi) #左对齐
  9. 3.142
  10. >>> Print ('%+f '% pi) #显示正负号
  11. +3.141593


4. How to make print non-line wrap
Always default line wrapping in Python

[Python]View PlainCopy
    1. >>> for x in range (0,10):   
    2.     print (x)   
    3.   
    4.       
    5. 0   
    6. 1  
    7. 2   
    8. 3  
    9. 4  
    10. 5  
    11. 6  
    12. 7  
    13. 8  
    14. 9  


If you want to not wrap, the previous version of 2.x can print x, plus at the end,
But it doesn't work in 3.x.
To change lines you should write print (X,end = ")

[Python]View PlainCopy
    1. >>> for x in range (0,Ten):
    2. print (x,end = ")
    3. 0123456789



Stitching strings:

[Python]View PlainCopy
  1. >>> "Hello""World"
  2. ' HelloWorld '
  3. >>> x = "Hello"
  4. >>> y = "World"
  5. >>> XY
  6. Traceback (most recent):
  7. File "<pyshell#10>", line 1, in <module>
  8. Xy
  9. Nameerror:name ' xy ' is not defined
  10. >>> X+y
  11. ' Helloworld '

Pow function:

[Python]View PlainCopy
    1. # 2**3%5 (2 of 3 power to 5 modulo)
    2. >>> Pow (2,3,5)
    3. 3


Then it's important that the type is free to convert, what value you assign, what type of variable, and Python will automatically manage it for you.

That's really turning my C + + thinking around.

[CPP]View PlainCopy
      1. >>> x = 2
      2. >>> type (x)
      3. <class ' int ' >
      4. >>> x = 2.3
      5. >>> type (x)
      6. <class ' float ' >
      7. >>> x = [2,3]
      8. >>> type (x)
      9. <class ' list ' >

Python Output with colororiginal September 29, 2016 15:04:51
    • Label:
    • python-Output/
    • Color
    • Python/
    • With color
    • 1881

\033[display mode; foreground color; background colour m

    1. Display mode
      0 (default value)
      1 (highlight)
      4 (underline)
      5 (flashing)
      7 (anti-display)
      22 (non-bold)
      24 (non-underlined)
      27 (non-inverting)

    2. Front view
      30 (Black)
      31 (red)
      32 (green)
      33 (yellow)
      34 (blue)
      35 (Magenta)
      36 (Cyan)
      37 (white)

    3. Background color
      40 (Black)
      41 (red)
      42 (green)
      43 (yellow)
      44 (blue)
      45 (Magenta)
      46 (Cyan)
      47 (White)

\033[1;31;40m Red
\033[1;32;40m Green

    1. How to use
      print ' \033[5;31;2m%s\033[0m '% num
      )
      print ' \033[5;34;2m%s\033[0m '% num

      print ' \033[5;35;2m%s\033[0m '% num

      ...
      ...
      ...
      There are many kinds of styles I don't give examples.

A detailed description of Python's print function

Related Article

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.