Example of print function usage in Python3.2

Source: Internet
Author: User
This example describes the use of the print function in Python3.2. Share to everyone for your reference. The specific analysis is as follows:

1. Output string

>>> Strhello = ' Hello World ' >>> print (Strhello) Hello World

2. Format the output integer

Supports parameter formatting, similar to the C-language printf

>>> Strhello = "The length of (%s) is%d"% (' Hello World ', Len (' Hello World ') >>> print (Strhello) the Len Gth of (Hello World) is 11

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)

Import math>>> print (' pi=%f '%math.pi) pi=3.141593>>> print ("PI =%10.3f"% math.pi) PI =   3.142> >> print ("PI =%-10.3f"% math.pi) pi = 3.142   >>> print ("PI =%06d"% int (math.pi)) pi = 000003

5. Formatted output floating-point number (float)

>>> precise = 3>>> print ("%.3s"% ("Python")) pyt>>> precise = 4>>> print ("%.*s"% ( 4, "Python")) pyth>>> print ("%10.3s"% ("Python"))    PYT

6. Output lists (list)

Output list

>>> lst = [1,2,3,4, ' Python ']>>> print (LST) [1, 2, 3, 4, ' Python ']

Output dictionary

>>> d = {1: ' A ', 2: ' B ', 3: ' C ', 4: ' d '}>>> print (d) {1: ' A ', 2: ' B ', 3: ' C ', 4: ' d '}

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

Or simply use the following function to output:

>>> Import sys>>> sys.stdout.write (' Hello World ') Hello World

Hopefully this article will help you with Python programming.

  • 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.