Python 輸入與輸出

輸入與輸出

輸入
  • python2
  1. raw_input()將用戶輸入作為'str'賦值給變量
  2. input()將用戶輸入以代碼形式原封不動的輸入到代碼中
  • python3
  1. input()相當于python2中的raw_input()函數(shù)
輸出
  • python2
  1. print xxx 直接輸出
name="aaa"
age=18
print "My name is %s,I am %d "%(name,age)
#輸出My name is aaa,I am 18
#索引輸出
print "My name is {0},I am {1} ".format(name,age)
#輸出My name is aaa,I am 18
#輸出到文件
f=open('test.txt','w')
print >>f,"輸出的內(nèi)容"
#使用分隔符分割
print "-".join(["a","b","c"])
#輸出a-b-c
  • python3
  1. print(values,sep,end,file,flush)函數(shù)
    values:要輸出的值,用逗號隔開
    sep:分隔符,多個值輸出,值與值之間使用的分隔符
    end:輸出完畢后以指定的符結(jié)束,默認為換行'\n'
    file:表示輸出的目標,默認標準是控制臺,可以是目標文件
    flush:表示立即輸出,默認為True
name="aaa"
age=18
print ("My name is %s,I am %d "%(name,age))
#輸出My name is aaa,I am 18
#索引輸出
print ("My name is {0},I am {1} ".format(name,age))
#輸出My name is aaa,I am 18
#輸出到文件
f=open("test.txt","w")
print ("輸出內(nèi)容",file=f)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容