如何自定义Python输出的格式?

时间:2015-09-23 09:13:29

标签: python

我基本上对python一无所知(之前我确实知道过,但从来没有花时间尝试过)。我开始使用python,因为我正在修读计算机科学的A级课程。

我想知道如何对齐输出?

我所做的“中心”对齐是:

print ("------(<< the spaces didnt show up in post, so i used "-"'s)How long have you been alive")

但我觉得使用它的技巧不对,我该怎么做呢? 此外,是否可以更改输出/运行视图的颜色 例如,标题为黑色,其他为蓝色,背景为黄色。

1 个答案:

答案 0 :(得分:1)

方法center()以长度为一的字符串返回居中。使用指定的fillchar完成填充。默认填充是一个空格。

<强>语法

str.center(width[, fillchar])

您可以参考下面的链接。

http://www.tutorialspoint.com/python/string_center.htm

https://docs.python.org/2/library/string.html#string.center

相关问题