python中expandtabs()函數的用法


練習題:制作表格

循環提示用戶輸入:用戶名、密碼、郵箱(要求用戶輸入的長度不能超過20個字符,如果超過則只有前20個字符有效),如果用戶輸入q或者Q表示不再繼續輸入,將用戶的內容一表格形式打印

s = ""
while True:
    v1 = input('請輸入你的名字')
    v2 = input('請輸入你的密碼')
    v3 = input('請輸入你的郵箱')
    v4 = v1[0:19]
    v5 = v2[0:19]
    v6 = v3[0:19]
    test = "{0}\t{1}\t{2}\n"
    v = test.format(v4, v5, v6)
    b = v.expandtabs(20)
    s = s + b
    if v1 == "q" or v2 == "q" or v3 == "q" or v1 == "Q" or v2 == "Q" or v3 == "Q":
        break
print(s)
​
請輸入你的名字q
​
請輸入你的密碼ww
​
請輸入你的郵箱ee
​
aa                  bb                  cc
​
dd                  dd                  dd
​
ff                  ff                  ff
​
q                   ww                  ee

  

expandtabs()函數
描述:返回一個字符串的副本。使原字符串中的制表符("\t")的使用空間變大。使用空格來擴展空間。

語法: str.expandtabs(tabsize=8)  —> str  返回字符串

tabsize 的默認值為8。tabsize值為0到7等效於tabsize=8。tabsize每增加1,原字符串中“\t”的空間會多加一個空格。

示例:

str = "this is\tstring example....wow!!!"
print(str.expandtabs())#默認值為8
print(str.expandtabs(tabsize=8))
print(str.expandtabs())
print(str.expandtabs(2)) #tabsize值為0到7,與tabsize值為8相同
print(str.expandtabs(tabsize=2))
print(str.expandtabs(tabsize=9))      
print(str.expandtabs(tabsize=10))

  運行結果:

this is string example....wow!!!
this is string example....wow!!!
this is string example....wow!!!
this is string example....wow!!!
this is string example....wow!!!
this is  string example....wow!!!
this is   string example....wow!!!

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM