Print format output of Python

Source: Internet
Author: User
Tags print format

Use print to output

    1. String
    2. Integer
    3. Floating Point Number
    4. Output and precision control
 
Strhello ='HelloPython' PrintStrhello# Output result: HelloPython# Directly output a string


1. format the output integer

PythonPrint also supports parameter formatting, which is similar to C-language printf,

Strhello ="The length of (% s) is % d" %('Hello world',Len('Hello world'))PrintStrhello# Output result: the length of (Hello World) is 11


2. Format and output a 16-digit integer

 
Nhex = 0x20# % X --- hex hexadecimal# % D --- decimal# % D --- Oct octal Print "Nhex = % x, ndec = % d, noct = % O" %(Nhex, nhex, nhex) # Output result: nhex = 20, ndec = 32, noct = 40# Print the same number in each integer format


3. format the output floating point number (float)

 Import   Math  # Default  Print   "Pi = % F"   %   Math . Pi  # Width = 10, precise = 3, align = left  Print   "Pi = % 10.3f"   %   Math . Pi  # Width = 10, precise = 3, align = rigoal  Print   "Pi = %-10.3f"   %  Math . Pi  # Prefill characters  Print   "Pi = % 06d"   %   Int  (  Math . Pi  )  # Output result  # Pig = 3.141593  # Pig = 3.142  # Pig = 3.142  # Pig = 000003  # Formatting, precision, degree, and 


4. format the output string (string)

# Precise = 3Print "%. 3 S" % ("Jcodeer")# Precise = 4Print "%. * S" % (4,"Jcodeer")# Width = 10, precise = 3Print "% 10.3 S" % ("Jcodeer")# Output result:# Jco# Jcod# Jco# Similar to strings, there are also precision, degree, and.


5. List)

L =[1,2,3,4,'Jcodeer']PrintL# Output result: [1, 2, 3, 4, 'jcodeer']# Print the list directly.'''6. Dictionary (dictionary )'''D ={1:'A',2:'B',3:'C',4:'D'}PrintD# Output result: {1: 'A', 2: 'B', 3: 'C', 4: 'D '}# SamePythonIt also supports dictionary output.


6. Print 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.

 
ForIIn Range(0,5):PrintI,

Or directly use the following function for output:

 
Sys.Stdout.Write("Output string")
 
Reprinted statement:This article is transferred fromHttp://www.pythonclub.org/python-basic/print
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.