輸入與輸出

0. 輸入


  • Python2.x
    # raw_input
    
    # 功能
      1. 會等待用戶輸入內(nèi)容,知道用戶按下 Enter
      2. 會將用戶輸入的內(nèi)容當(dāng)做 字符串,傳遞給接收的變量
    
    inputStr = raw_input("請輸入信息")
    print type(inputStr)
    print inputStr
    
    # input
    
    # 功能
      1. 會等待用戶輸入內(nèi)容,直到用戶按下 Enter
      2. 會將用戶輸入的內(nèi)容當(dāng)做 代碼 進(jìn)行處理
    
    inputResult = input("請輸入代碼")
    print type(inputResult)
    print inputResult
    
  • Python3.x
    # input
    
    # 功能
      1. 會等待用戶輸入內(nèi)容,直到用戶按下 Enter
      2. 會將用戶輸入的內(nèi)容當(dāng)做 字符串,傳遞給接收的變量
     
    inputStr = input("請輸入消息")
    print(type(inputStr))
    print(inputStr)
    
    inputResult = eval(inputStr)
    print(type(inputResult))
    print(inputResult)
    

1. 輸出


  • Python2.x
    # print 語句:print xxx
    print "my name is luck"
    
  • Python3.x
    # print函數(shù):print(values, sep, end, file, flush) 
    
    # values:需要輸出的值
      1. 多個(gè)值,使用 , 進(jìn)行分割
    
    # sep:分割符
      1. 多個(gè)值,被輸出出來之后,值與值之間,會添加指定的分隔符
      2. 默認(rèn)值 ' '
    
    # end:輸出完畢之后,以指定的 字符 結(jié)束
      1. 默認(rèn)是換行 \n
    
    # file:表示 輸出的目標(biāo)
      1. 默認(rèn)是標(biāo)準(zhǔn)的輸出,即控制臺,file = sys.stdout
      2. 還可以是一個(gè)可寫入的文件句柄,file = open("luck.txt", "w")
    
    # flush:表示 是否立即 輸出
      1. 流程
        (1) 需要輸出的內(nèi)容,先存放在 緩沖區(qū),然后再輸出到目標(biāo)
        (2) flush,就代表是否刷新緩沖區(qū),讓緩沖區(qū)的內(nèi)容,立即輸出到目標(biāo)
      2. 若輸出內(nèi)容末尾是以 \n 結(jié)尾,則會 立即刷新 緩沖區(qū)
      3. 默認(rèn)是 False
    

2. 占位格式符


  • 格式:%[(name)][flags][width][.precision]typecode
    • 中括號 [] 包含的部分,代表可選
  • (name):用于選擇指定的名稱對應(yīng)的值
    age = 20
    luckAge = 18
    print("我年齡是:%(my)d,luck 年齡是:%(luck)d" % ({"my": age, "luck": luckAge}))
    
  • flags
    • :表示 右對齊,在 左側(cè) 空出位置
      firstNum = 20
      secondNum = 18
      print("%10d%d" % (firstNum, secondNum))
      
    • -:表示 左對齊,在 右側(cè) 空出位置
      firstNum = 20
      secondNum = 18
      print("%-10d%d" % (firstNum, secondNum))
      
    • 空格:表示在 正數(shù)左側(cè) 填充 一個(gè)空格,從而與 負(fù)數(shù)對齊
      firstNum = 20
      secondNum = 18
      print("% d%d" % (firstNum, secondNum))
      
    • 0:表示使用 0 填充 左側(cè)
      minutes = 5
      sec = 18
      print("%02d:%02d" % (minutes, sec))
      
  • width:表示 顯示寬度
    firstNum = 20
    secondNum = 18
    print("%10d%d" % (firstNum, secondNum))
    
  • .precision:表示小數(shù)點(diǎn)后 精度
    score = 59.9
    print("%.5f" % score)
    
  • typecode轉(zhuǎn)換說明符
    • 數(shù)值
      數(shù)值.png
    • 字符串
      • s:獲取傳入對象的 _str_ 方法的返回值,并將其格式化到指定位置
      • r:獲取傳入對象的 _repr_ 方法的返回值,并將其格式化到指定位置
      • c
        # 整數(shù):將數(shù)字轉(zhuǎn)換成其 unicode 對應(yīng)的值
        # Python2.x,只支持 0 ~ 255
        # Python3.x,10 進(jìn)制范圍支持 0 ~ 1114111
        print("我就是很厲害啊%c" % 19999)
         
        # 字符:將字符添加到指定位置
        print("我就是很厲害啊%c" % "哦")
        
    • 注意
      • %% 表示一個(gè) %
      • 不存在 自動將 整數(shù) 轉(zhuǎn)換成 二進(jìn)制 表示的方式

3. 應(yīng)用場景


  • 輸出一個(gè)值
    # Python2.x
    print 666
    
    # Python3.x
    print(3)
    
  • 輸出一個(gè)變量
    # Python2.x
    num = 88
    print num
    
    # Python3.x
    num = 66
    print(num)
    
  • 輸出多個(gè)變量
    # Python2.x
    a, b, c = 66, 88, 100
    print a, b, c
    
    # Python3.x
    a = b = c = 100
    print(a, b, c)
    
  • 格式化輸出
    # Python2.x 
    
    # % 寫法
    name = "秦子陽"
    age = 18
    formatStr = "my name is %s, and age is %d" % (name, age)
    print type(formatStr), formatStr
    
    # format 寫法
    name = "秦子陽"
    age = 18
    formatStr = "my name is {0}, and age is {1}".format(name, age)
    formatStr = "my name is {a}, and age is ".format(a=name, b=age)
    print type(formatStr), formatStr
    
    # Python3.x
    
    # % 寫法
    name = "小熊bliving"
    age = 18
    formatStr = "my name is %s, and age is %d" % (name, age)
    print(type(formatStr), formatStr)
    
    # format 寫法
    name = "小熊bliving"
    age = 18
    formatStr = "my name is {0}, and age is {1}".format(name, age)
    formatStr = "my name is {a}, and age is ".format(a=name, b=age)
    print(type(formatStr), formatStr)
    
  • 輸出到文件中
    # Python2.x 
    filePath = open("luck.txt", "w")
    print >> filePath, "my name is 秦子陽, and age is 18."
    
    # Python3.x
    filePath = open("luck.txt", "w")
    print("my name is 秦子陽, and age is 18", file=filePath)
    
  • 輸出不自動換行
    # Python2.x 
    a = b = c = 100
    print a,
    print b, 
    print c,
    
    # Python3.x
    print("hello,", "i am luck.", end="")
    
  • 輸出的各個(gè)數(shù)據(jù),使用分隔符分隔
    # Python2.x 
    print "_".join(["a", "b", "c"])
    
    # Python3.x
    print("my", "name", "is", "luck", sep="---")
    

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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