注册 登录
编程论坛 Python论坛

python输出格式问题

lonmaor 发布于 2010-07-12 11:53, 2415 次点击
例5.1 使用表达式

程序代码:
#!/usr/bin/python
#
Filename: expression.py

length = 5
breadth = 2
area = length * breadth
print 'Area is', area
print 'Perimeter is', 2 * (length + breadth)


(源文件:code/expression.py)

输出
$ python expression.py
Area is 10
Perimeter is 14

另外,注意Python如何打印“漂亮的”输出。尽管我们没有在'Area is'和变量area之间指定空格,Python自动在那里放了一个空格,这样我们就可以得到一个清晰漂亮的输出,而程序也变得更加易读(因为我们不需要担心输出之间的空格问题)。这是Python如何使程序员的生活变得更加轻松的一个例子。


请问如何避免这种自作聪明的格式输出,以便以后如果输出1rd,2nd,3th...
print 'I\'m the', num,'th',one.
7 回复
#2
lonmaor2010-07-12 14:29
刚看到了相关的格式化输出
程序代码:

age = 22
name = 'Swaroop'

print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name

原来可以这样
#3
外部三电铃2010-07-12 17:55
.......
#4
yangfanconan2010-07-12 20:11


#5
文竹一叶2010-07-24 19:15
我用的是名为《简明Python教程》的一本Python入门书籍,刚刚开始学,上有一段代码:
print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
    print item,
意思是输出时的“These items are :”后面不应该有空格,但我在Python3.1自带的编程环境中得到的输出中仍然有换行...
#6
文竹一叶2010-08-14 21:28
找到了,print("Right!",end='')
#7
yexin2182010-08-16 09:25
呵呵  不错
#8
lucky5635912010-11-21 07:56
公交很慢啊!
1