A detailed 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

?

1 2 3 >>> Strhello = "Hello World" >>> print (Strhello) Hello World

2. Formatted output integer

Support parameter formatting, similar to the C language printf

?

1 2 3 >>> Strhello = "The length of (%s) is%d"% (' Hello World ', Len (' Hello World ')) >>> print (Strhello) The L Ength of (Hello World) is 11

3. Formatted output 16, decimal, octal integers

#%x---hex hex

#%d---Dec decimal

#%o---Oct octal

?

1 2 3 >>> Nhex = 0xFF >>> print ("Nhex =%x,ndec =%d,noct =%o"% (nhex,nhex,nhex)) Nhex = Ff,ndec = 255,noct = 377

4. Formatted output floating-point number (float)

?

1 2 3 4 5 6 7 8 9 Import Math >>> print (' pi=%f '%math.pi) pi=3.141593 >>> print ("PI =%10.3f"% math.pi) PI = 3.142 >&G t;> print ("PI =%-10.3f"% math.pi) pi = 3.142 >>> print ("PI =%06d"% int (math.pi)) pi = 000003

5. Formatted output floating-point number (float)

?

1 2 3 4 5 6 7 8 >>> precise = 3 >>> print ("%.3s"% ("python") pyt >>> precise = 4 >>> print ("%.*s" % (4, "Python")) Pyth >>> print ("%10.3s"% ("python") PYT

6. Output lists (list)

Output list

?

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

Output dictionary

?

1 2 3 >>> 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 do not need to enter, simply add a comma "," at the end of the print statement, and you can change its behavior.

?

1 2 3 4 5 6 7 8 9 >>> for I in Range (0,6): Print (I,) 0 1 2 3 4 5

or use the following function directly to output:

?

1 2 3 >>> Import sys >>> sys.stdout.write (' Hello World ') Hello World

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.