1. 程式人生 > >python輸入與輸出

python輸入與輸出

什麽 字符 模式 定義 中間 rep eba 精度 傳遞

python輸出

python3中的輸出

python3中的輸出使用函數print(),示例如下:

>>> print(‘hello kitty‘)

print()也可接受多個參數,使用逗號隔開:

>>> 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中的輸出

python2中的輸出使用print 加上 輸出數據,示例如下:

>>> print ‘hello kitty‘

也可以接收多個參數:

>>> print ‘1+2 =‘,3
1+2 = 3

用法與python3中的print()函數基本相同~

python格式化輸出

格式化輸出字符串

>>> print(‘My name is %s‘ % (‘abc‘))
My name is abc

%表示格式化操作,% 前面的字符串中的%s(格式符) 使用 % 後面的字符串 ‘abc‘ 替換。

打印整數:

>>> 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 !!

python輸入

python3中的輸入

python3中的輸入使用input(),將用戶在終端的輸入,存放到一個變量中

>>> name=input()
hello
>>> name
‘hello‘

input() 可以帶上一個參數,作為用戶輸入時的提示信息,示例如下:

>>> name = input("What is your name?")
What is your name?abc
>>> name
‘abc‘

Tip:input() 會將用戶輸入的數據都當做字符串(str)進行處理~

>>> lst = input()
[1,2,3,4,5]
>>> type(lst)
<class ‘str‘>

python2中的輸入

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() 在接收用戶輸入的數據時,輸入的是什麽類型,就存放為什麽類型。註意區別
.................^_^

python輸入與輸出