Example of Print function usage in Python3.2

Source: Internet
Author: User
This article mainly introduces the usage of the Print function in Python3.2 and analyzes in detail the Print function Output Techniques in Python3.2 in the form of examples, which has some reference value, for more information about how to use the Print function in Python3.2, see the following example. Share it with you 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 printf in C Language

>>> strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))>>> print (strHello)the length of (Hello World) is 11

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)

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. format the output floating point number (float)

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

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

Or directly use the following function for output:

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

I hope this article will help you with Python programming.

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.