Python:尝试通过在名片上移动外部边框来获取用户输入

标签 python border whitespace indentation quotes

first_name = input("What is your first name?")

last_name = input("What is your last name?")

phone_number = input("What is your phone #?")

one = ('|')
two = ('|')
print('Here is your business card...')

business_card = """
+------------------------------------------------+
|    |                                           |
|   -|          %s, %s                           %s
|  --|          Tribute Liabilities Associate    |
| ---|          Parasail Capital                 |
| ---------                                      |
|  -------      4 Hunger Plaza                   |
|               STE 1400                         |
|               District 12, Panem 00012         | 
|                                                |
| Work: %s  @: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e7848f868a97a79786958694868e8bc984888a" rel="noreferrer noopener nofollow">[email protected]</a>                %s
+------------------------------------------------+""" % (first_name, last_name, one , phone_number, two)



print (business_card)

print('Here is your business card...')

尝试在不移动边框的情况下在名片上输入名字/姓氏和电话号码。由于姓名和/或电话号码产生的空白,它会自动移动边框(向左或向右)......有什么想法吗?

最佳答案

如果您使用 format() 字符串方法,您可以告诉它在您输入的字符串的两侧放置多少空白。

以下是 format() 涉及的迷你语言的文档: https://docs.python.org/2/library/string.html#formatspec

因此,当您输入名称时,您会执行类似 "{:15}".format(name) 的操作,其中 15 是您允许的空格数填充(输入默认左对齐)。这为您提供了比 "%s"% (name) 更多的控制权,并且可以更轻松地进行格式化。

一般来说,format() 文档将提供有关如何动态格式化字符串的大量信息。

关于Python:尝试通过在名片上移动外部边框来获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28292838/

相关文章:

python - Tensorflow InvalidArgumentError : 2 root error(s) found. 索引 [28,0] = 11292 不在 [0, 11272)

wpf - DynamicResource 颜色对边框上的 BorderBrush 不起作用 - 错误?

java - 我可以制作无边框的 JTable 列吗?

string - 从 perl 变量中删除一个空格

excel - 如果字段为空,则将 CONCATENATE 向上挤压,这样空间间隙就不会太大

python - 如何在 reStructuredText 的代码块中强制使用空格

python - 在 gensim Word2Vec 模型中匹配单词和向量

python子进程dpkg : I'm getting errors for something that runs in bash

python - python中数组的答案

css - Chrome 1px 边框根据窗口高度变化