Python 字符串的 center() 对齐不正确
Improper center() alignment of Python strings
我正在开发一个显示基本用户详细信息的项目。
这是我遇到问题的相应代码。
@wrap
def show(self, *ids):
for id in ids:
try:
user = self.api.get_user(id)
print(user.name.center(80, '~'))
print('%d Followers %d Following'.center(80, '~') %
(user.followers_count, user.friends_count))
except Exception:
print('Error')
finally:
print()
代码显示了最大宽度为 80 个字符的两行。
此时它每行最多只能打印 80 个字符。
但是我得到了这个输出:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Twitter~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~58312564 Followers 1 Following~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一行看起来很完美,但第二行真的不看中心。
I read the docs and I also tried using .format() inside the print statement but I'm getting the same problem.
如何在中间打印两行内容?
(一字之差相当可观)
请帮忙。
这对你有用吗?
followers_count= 5831256
friends_count = 1
var = f" {followers_count} Followers {friends_count} Following "
print(f"{' Twitter ':~^80}")
print(f"{var:~^80}")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Twitter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~ 5831256 Followers 1 Following ~~~~~~~~~~~~~~~~~~~~~~~~
根据您的方法定义对其进行调整。
注意:var 是不必要的
我正在开发一个显示基本用户详细信息的项目。
这是我遇到问题的相应代码。
@wrap
def show(self, *ids):
for id in ids:
try:
user = self.api.get_user(id)
print(user.name.center(80, '~'))
print('%d Followers %d Following'.center(80, '~') %
(user.followers_count, user.friends_count))
except Exception:
print('Error')
finally:
print()
代码显示了最大宽度为 80 个字符的两行。 此时它每行最多只能打印 80 个字符。 但是我得到了这个输出:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Twitter~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~58312564 Followers 1 Following~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一行看起来很完美,但第二行真的不看中心。
I read the docs and I also tried using .format() inside the print statement but I'm getting the same problem.
如何在中间打印两行内容? (一字之差相当可观)
请帮忙。
这对你有用吗?
followers_count= 5831256
friends_count = 1
var = f" {followers_count} Followers {friends_count} Following "
print(f"{' Twitter ':~^80}")
print(f"{var:~^80}")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Twitter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~ 5831256 Followers 1 Following ~~~~~~~~~~~~~~~~~~~~~~~~
根据您的方法定义对其进行调整。 注意:var 是不必要的