python的輸入和輸出
阿新 • • 發佈:2018-12-18
print()函式的使用
無論什麼型別的變數,數值,布林,列表,字典…都可以直接輸出
一、列印字串
python遇到逗號,會輸出空格
print("hello,world!") #接受一個字串
print("are","you","ok") #接受多個字串,會以空格分隔輸出
二、列印整數
print(5)
print(3+2)
三、格式化輸出
- %s——字串格式化
name="小明"
print("hello,%s"%name)
- %d——整數格式化
age=21 print("The age is %d"%age)
- 多個變數格式化
name="小明"
age=21
print("The age of %s is %d" %(name,age))
四、使輸出不換行
print預設是換行的
新增 ,end="" 可以使輸出不換行
print(100,end="")
print(5)
input()函式的使用
input函式,以字串的形式獲取鍵盤輸入
一、獲取字串
input()會得到一個字串變數
user=input()
print(user)
二、獲取字串,並做提示
content=input("請輸入內容") print(content)
三、獲取整數(需型別轉換)
number=int(input())
print(number)
print(type(number))