<span style="background-color: rgb(255, 255, 255);"><span style="font-family:SimSun;font-size:24px;">通常,当你想看变量内容时,在代码中使用 print 语句输出。在 shell 中,也可以用 print 语句,或者直接使用变量名。</span></span>
下划线(underscore)表示最后一个表达式的值。
与C语言类似,print 语句可以结合 % 使用。%s 表示字符串,%d 表示整型,%f 表示浮点型。
print("%s is number %d!"% ("Python",1))
使用 str.format( ) 方法:{0}对应format中第一个字符串,{1}对应format中第二个字符串……
1
|
print ( '{0} like {1}' . format ( 'Jason' , 'apple' ))
|
str.format( ) 中还可以定义变量。
1
|
print ( '{0} and {other}.' . format ( 'you' ,other = 'me' ))
|
配合使用 for 循环:范围(1<=x<11),2d代表显示整型2位,4d代表显示整型4位……对齐排列
1
2
|
for x in range ( 1 , 11 ):
print ( '{0:2d} {1:3d} {2:4d}' . format (x, x * x, x * x * x))
|