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}	{1}	{2}
"
    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()函数
描述:返回一个字符串的副本。使原字符串中的制表符(" ")的使用空间变大。使用空格来扩展空间。

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

tabsize 的默认值为8。tabsize值为0到7等效于tabsize=8。tabsize每增加1,原字符串中“ ”的空间会多加一个空格。

示例:

str = "this is	string 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!!!

  

原文地址:https://www.cnblogs.com/wxcx/p/12702154.html