用Python打印表格 [英] Printing a table in Python

查看:64
本文介绍了用Python打印表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分配了一个用Python创建10 x 10的表的功能,并且在打印功能中使用了结尾的\\来防止它创建新行.但是,我需要它在10个字符之后开始新的一行.我该怎么做呢?这是我的代码:

I have an assignment to create a 10 by 10 table in Python and I'm using the end "\t" within my print function to prevent it from creating a new line. But, I need it to start a new line after of course 10 characters. How can I make it do that? Here's my code:

product = 0
for x in range(1,11):
    for y in range(1,11):
        product = x * y
        print(product, end="\t")

我需要它看起来像这样:

I need it to look something like this:

1   2   3   4   5   6   ...
2   4   6   8   10  12  ...
3   6   9   12  15  18  ...

推荐答案

这是使用 format 方法中的 alignment 选项.

This is using alignement options from the format method.

product = 0
for x in range(1,11):
    for y in range(1,11):
        product = x * y
        print('{:<4}'.format(product), end='')
    print()

参考:格式规范迷你语言

这篇关于用Python打印表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆