1. 程式人生 > >Python中的輸入和輸出列印

Python中的輸入和輸出列印

以下的都是在Python3.X環境下的

使用 input 函式接收使用者的輸入,返回的是 str 字串

最簡單的列印

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