Python3.2中Print函數(shù)用法實(shí)例詳解
本文實(shí)例講述了Python3.2中Print函數(shù)用法。分享給大家供大家參考。具體分析如下:
1. 輸出字符串
>>> strHello = 'Hello World' >>> print (strHello) Hello World
2. 格式化輸出整數(shù)
支持參數(shù)格式化,與C語言的printf類似
>>> strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print (strHello)
the length of (Hello World) is 11
3. 格式化輸出16進(jìn)制,十進(jìn)制,八進(jìn)制整數(shù)
#%x --- hex 十六進(jìn)制
#%d --- dec 十進(jìn)制
#%o --- oct 八進(jìn)制
>>> nHex = 0xFF
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = ff,nDec = 255,nOct = 377
4.格式化輸出浮點(diǎn)數(shù)(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. 格式化輸出浮點(diǎn)數(shù)(float)
>>> precise = 3
>>> print ("%.3s " % ("python"))
pyt
>>> precise = 4
>>> print ("%.*s" % (4,"python"))
pyth
>>> print ("%10.3s " % ("python"))
pyt
6.輸出列表(List)
輸出列表
>>> lst = [1,2,3,4,'python'] >>> print (lst) [1, 2, 3, 4, 'python']
輸出字典
>>> d = {1:'A',2:'B',3:'C',4:'D'}
>>> print(d)
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
7. 自動換行
print 會自動在行末加上回車,如果不需回車,只需在print語句的結(jié)尾添加一個逗號”,“,就可以改變它的行為。
>>> for i in range(0,6): print (i,) 0 1 2 3 4 5
或直接使用下面的函數(shù)進(jìn)行輸出:
>>> import sys
>>> sys.stdout.write('Hello World')
Hello World
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Python字符串函數(shù)strip()原理及用法詳解
這篇文章主要介紹了Python字符串函數(shù)strip()原理及用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
VSCODE配置Markdown及Markdown基礎(chǔ)語法詳解
這篇文章主要介紹了VSCODE配置Markdown及Markdown基礎(chǔ)語法詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
使用Python模塊進(jìn)行數(shù)據(jù)處理的詳細(xì)步驟
Python 提供了豐富的模塊和庫,用于處理各種類型的數(shù)據(jù),本文介紹了一些常用的模塊和庫,以及如何使用它們進(jìn)行數(shù)據(jù)處理的詳細(xì)步驟和代碼示例,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-02-02
在Mac OS系統(tǒng)上安裝Python的Pillow庫的教程
這篇文章主要介紹了在MacOS下安裝Python的Pillow庫的教程,Pillow庫用來對圖片進(jìn)行各種處理操作,需要的朋友可以參考下2015-11-11

