目錄
最簡單的列印
列印數字
列印字元
字元串的格式化輸出
python中讓輸出不換行
以下的都是在Python3.X環境下的
使用 input

最簡單的列印
>>print("hello,word!")
hello,word!
列印數字
>>a=5
>>b=6
>>print(a)
>>print(a,b)
>>print(a+b)
5
5 6
11
列印字元
使用逗号連接配接會有空格,使用+号連接配接沒有空格
>>a="hello,"
>>b="world!"
>>print(a,b)
>>print(a+b)
hello, world!
hello,world!
特别注意,當字元串是等于一個數的時候,這樣兩個字元串相加還是字元串。要把字元串轉化為數字類型的才可以使用相加
>>a=input("請輸入第一個數字:") #20
>>b=input("請輸入第二個數字: ") #10
>>print(a,b)
>>print(a+b)
>>print(int(a)+int(b))
20 10
2010
30
字元串的格式化輸出
>>name="小謝"
>>age="20"
>>print("{}的年齡是{}".format(name,age))
>>print("%s的年齡是%s"%(name,age))
小謝的年齡是20
小謝的年齡是20
>>print("i have a {1} and have a {0}".format("apple","orange"))
i have a orange and have a apple
>>print("i have a {one} and have a {two}".format(one="apple",two="orange"))
i have a apple and have a orange
>>print("i have a {} and have a {two}".format("apple",two="orange"))
i have a apple and have a orange
>>import math
>>print("{1:.3f} and {0.4f}".format(math.pi,math.e))
2.718 and 3.1416
#自動填充
>>print('12'.zfill(5))
>>print('-3.14'.zfill(7))
00012
-003.14
>>print('Hi,%s!'%input('Please enter your name!')) //接收使用者的輸入,然後列印出來
Please enter your name!xie // xie 是使用者輸入的
Hi,xie!
pprint列印
pprint子產品用于列印 Python 資料結構. 當你列印特定資料結構時你會發現它很有用(輸出格式比較整齊, 便于閱讀)
import pprint
data = (
"this is a string", [1, 2, 3, 4], ("more tuples",
1.0, 2.3, 4.5), "this is yet another string"
)
print(data)
print("*"*100)
pprint.pprint(data)
######################################
('this is a string', [1, 2, 3, 4], ('more tuples', 1.0, 2.3, 4.5), 'this is yet another string')
******************************************************************************************
('this is a string',
[1, 2, 3, 4],
('more tuples', 1.0, 2.3, 4.5),
'this is yet another string')