python3中的輸出使用函數print(),示例以下:python
>>> print('hello kitty')
print()也可接受多個參數,使用逗號隔開:ide
>>> print('hello','kitty') hello kitty
能夠看到字符串合併輸出後,中間會模式使用逗號隔開~函數
print函數除了能夠接收字符串外,也能夠接收其餘的數據類型調試
>>> print(1) # 接收整數 1 >>> print(1+2) # 表達式 3 >>> print([1,2,3]) # 列表 [1, 2, 3] >>> print({'a':1,'b':2}) # 字典 {'a': 1, 'b': 2}
python2中的輸出使用print 加上 輸出數據,示例以下:code
>>> print 'hello kitty'
也能夠接收多個參數:orm
>>> print '1+2 =',3 1+2 = 3
用法與python3中的print()函數基本相同~ip
格式化輸出字符串ci
>>> print('My name is %s' % ('abc')) My name is abc
%表示格式化操做,% 前面的字符串中的%s(格式符) 使用 % 後面的字符串 'abc' 替換。字符串
打印整數:get
>>> print("I'm %d year old" % 18) # 當只有一個值的時候,能夠不適用小括號 I'm 18 year old
多個格式符:
>>> print("I'm %s. I'm %d year old" % ('abc', 18)) I'm abc. I'm 18 year old
多個格式符也能夠使用字典來傳遞值:
>>> print("I'm %(name)s. I'm %(age)d year old" % {'name':'abc', 'age':18}) I'm abc. I'm 18 year old
格式符
%s 字符串 (採用str()的顯示) %r 字符串 (採用repr()的顯示) %c 格式化字符及其ASCII碼 %b 二進制整數 %d 十進制整數 %u 格式化無符號整型 %o 格式化無符號八進制數 %x 格式化無符號十六進制數 %X 格式化無符號十六進制數(大寫) %e 用科學計數法格式化浮點數 %E 做用同%e,用科學計數法格式化浮點數 %f 格式化浮點數字,可指定小數點後的精度 %g %f和%e的簡寫 %G %f 和 %E 的簡寫 %% 字符"%"
格式符爲真實值預留位置,並控制顯示的格式。
能夠用以下的方式,對格式進行進一步的控制:
%[(name)][flags][width].[precision]typecode
(name)爲命名
flags能夠有-,' '或0。若不寫默認表示右對齊。- 表示左對齊。' '爲一個空格,表示在正數的左側填充一個空格,從而與負數 對齊。0表示使用0填充。
width表示顯示寬度
precision表示小數點後精度
示例以下:
>>> print("%4d" % 5) # flags不填(默認右對齊),width爲4(總長爲4位) 5 >>> print("%-4d" % 5) # flags爲 - ,表示左對齊 5 >>> print("%06d" % 5) # 總長爲6位,flags爲0,即左邊使用0填充 000005 >>> print('-- %f --' % (1.23)) # 格式化浮點數 -- 1.230000 -- >>> print('-- %5.2f --' % (1.2345)) # 總長5位,小數點後保留2位 -- 1.23 -- >>> print('-- %05.2f --' % (1.2345)) # 總長5位,小數點後保留2位,flags爲0,左邊使用0填充(小數點也佔一位) -- 01.23 --
Python中還有另外一種格式化方式,利用format,這也是官方推薦的方式:
方式一: >>> print("My name is {0}. I'm {1} year old. Hello {0} !!".format('baby', 18)) My name is baby. I'm 18 year old. Hello baby !! 方式二: >>> print("My name is {name}. I'm {age} year old. Hello {name} !!".format(name='baby', age=18)) My name is baby. I'm 18 year old. Hello baby !!
python3中的輸入使用input(),將用戶在終端的輸入,存放到一個變量中
>>> name=input() hello >>> name 'hello'
input() 能夠帶上一個參數,做爲用戶輸入時的提示信息,示例以下:
>>> name = input("What is your name?") What is your name?abc >>> name 'abc'
當輸入的內容爲密碼之類的數據時,能夠使用getpass模塊,隱藏輸入的數據:
>>> import getpass >>> pwd=getpass.getpass('Enter your password:') Enter your password: # 不顯示密碼 >>> pwd 'abcd'
不過這個貌似只能在python的交互模式下才能調試,在pycharm中沒法實現~
Tip:無論用戶輸入的數據看上去是什麼類型的,input() 都會當作字符串(str)進行處理~
>>> lst = input() [1,2,3,4,5] >>> type(lst) <class 'str'> >>> lst '[1,2,3,4,5]' # 注意兩邊的單引號,這是一個字符串,而不是列表
python2中的raw_input用法與python3中的input() 相似:
>>> age = raw_input("How old are you?") How old are you?12 >>> type(age) <type 'str'>
Tip:raw_input也同樣,會將用戶輸入的數據都當作字符串(str)處理。
python2中還能夠用 input() 來接收用戶的輸入,這裏的 input() 用法與python3中的 input() 有所區別
>>> name = input("What is your name?") What is your name?baby # 這裏輸入的是 變量 baby,而不是字符串,因爲 baby 變量沒有定義,因此報錯 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'baby' is not defined >>> name = input("What is your name?") What is your name?'baby' # 這裏輸入的是 字符串 'baby',成功賦值~ >>> lst = input() [1,2,3,4,5] # 輸入的是 列表類型,lst變量即爲列表~ >>> type(lst) <type 'list'>
Tip:python2中的 input() 在接收用戶輸入的數據時,輸入的是什麼類型,就存放爲何類型。注意區別.................^_^