banner ad

Python3.2 — Print function

| January 19, 2013 | 0 Comments
0 Flares 0 Flares ×

1.string output

>>> StrHello = 'Hello World'
>>> Print (strHello)

Hello World
2. Formatted integer

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

the length of (Hello World) is 11
Formatted output in hexadecimal, decimal, octal integer
#% x — hex
#% d — dec
#% o — oct
 

>>> NHex = 0xFF
>>> Print ("nHex =% x, nDec =% d, nOct =% o"% (nHex, nHex, nHex))

nHex = ff, nDec = 255, nOct = 377

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
Formatted output floating point number (float)
>>> Precise = 3
>>> Print ("% .3 s"% ("python"))
pyt
>>> Precise = 4
>>> Print ("%. * S"% (4, "python"))
pyth
>>> Print ("% 10.3s"% ("python"))
       pyt
Output list (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'}


>>> For i in range (0,6):
    print (i,)
    
0
1
2
3
4
5
Or directly using the output of the following function:

>>> Import sys
>>> Sys.stdout.write ('Hello World')
Hello World
Download PDF
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×

Tags: , ,

Category: String

About the Author ()

My name is John Link.I am 26 years old. My major is Computer science and technology. I am a junior programmer with Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×