格式化输入语句的输出

时间:2017-07-17 14:17:43

标签: python

我想格式化这个输出,其中括号中的数字是偶数,并且与括号中的其他数字成直线排列。示例输出如下所示

# AStE ....................(1)
# AST......................(2)
#ZASKW.....................(3)
#gREEENN...................(4)
# THESE ARE EXAMPLE NAMES WITH MORE LETTERS IN THEM.



location = raw_input("\n \n THIS IS A LIST OF words with ID'S ASSIGNED TO THEM () \n ASTE (1)\n Ast (2) \n AS (3) \n ASTO (4) \n Bro (5) \n Cor (6) \n DUn (7) \n DUNWO (8) \n Ea (9) \n Eas (10) \n"" VI (11) \n Green (12) \n Mill (13) \n State (14) \n Ver (15) \n We (16) \n PLEASE ENTER THE ID # ")

1 个答案:

答案 0 :(得分:0)

它应该按照你的意愿行事。

commands = ["AStE", "AST", "ZASKW", "gREEENN"]
max_columns = 30

for index, commands in enumerate(commands):
    stars_amount = max(max_columns - len(commands), 0)
    row = "# {} {}({})".format(commands, "." * stars_amount, index + 1)
    print row
print "PLEASE ENTER THE ID:"

输出:

# AStE ..........................(1)
# AST ...........................(2)
# ZASKW .........................(3)
# gREEENN .......................(4)
PLEASE ENTER THE ID:

基于@ason​​gtoruin评论的替代解决方案:

commands = ["AStE", "AST", "ZASKW", "gREEENN"]

for index, commands in enumerate(commands):
    row = "# {:.<30} ({})".format(commands, index + 1)
    print row
print "PLEASE ENTER THE ID:"