[英]basic formatting a strings in python
Any idea why the count1 variable doesn't format to align to the right?知道为什么 count1 变量没有格式化为右对齐吗?
count1 = 5
games = 7
print(format("correct guesses: %s" %(count1), '>5s'))
print(format("this is how many games played: %s" %(games), '>5s'))
output should be so both numbers align together on the right side so something like this: output 应该是这样两个数字在右侧对齐所以像这样:
correct guesses: 5
this is how many games played: 7
You need to apply formatting to the string as if there were 2 columns of data:您需要将格式应用于字符串,就好像有 2 列数据一样:
count1 = 5
games = 7
print("correct guesses:".ljust(30), format("%s" % count1, '>5s'))
print("this is how many games played:".ljust(30), format("%s" % games, '>5s'))
Or using f-strings:或者使用 f 弦:
print(f"{'correct guesses:':<30} {count1:>5}")
print(f"{'this is how many games played:':<30} {games:>5}")
Output: Output:
correct guesses: 5
this is how many games played: 7
Thank you I got it to work with the following:谢谢你,我得到它与以下工作:
count1 = 5
games = 7
print(("correct guesses: "), format("%g" %count1, '>19s'))
print(("This is how many games played: "), format("%g" %games, '>5s'))
output was:
correct guesses: 5
This is how many games played: 7
19 was used to ensure the two lined up due to the extra characters in the string.由于字符串中的额外字符,19 用于确保两个对齐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.