print() 方法用于打印输出,是python中最常见的一个函数。
该函数的语法如下:
print(*objects, sep="", end="\n", file=sys.stdout)
参数的具体含义如下:
objects --表示输出的对象。输出多个对象时,需要用 , (逗号)分隔。
sep -- 用来间隔多个对象。
end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。
file -- 要写入的文件对象。
# a = "hello"
# b = "world"
# print(a, b)
# hello world
# print("hello""world")
# helloworld
# print("hello", "world")
# hello world
# print("www", "9iphp", "cn", sep=".")
# www.9iphp.cn
# 格式化输出
# hello = "the length of %s is %d" % ("hello", len("hello"))
# print(hello)
# the length of hello is 5
# PI = 3.141592653
# 字段宽10,精度3
# print("%10.3f" % PI)
# 3.142
# PI = 3.141592653
# print("PI=%.*f" % (3, PI))
# PI=3.142
# PI = 3.141592653
# print("PI=%*.3f" % (10, PI))
# PI= 3.142
# 使用位置和关键字格式化字符串
# print("恭喜{}获得{}分".format("亮仔", 100))
# 恭喜亮仔获得100分
# print("{score}分由{name}获得".format(name = "亮仔", score = 100))
# 100分由亮仔获得
# print("{} {}".format("hello", "world"))
# hello world
# print("{0} {1}".format("hello", "world"))
# hello world
# print("{0} {1} {0}".format("hello", "world"))
# hello world hello
# print("{a} {tom} {a}".format(tom="hello", a="world"))
# world hello world
# < 默认左对齐
# print("{} and {}".format("hello", "world"))
# hello and world
# 取10位左对齐,取10位右对齐(>)
# print("{:<10s} and {:>10s}".format("hello", "world"))
# hello and world
# 取10位中间对齐(^)
# print("{:^10s} and {:^10s}".format("hello", "world"))
# hello and world
# 取2位小数
# print("{} is {:.2f}".format(1.123, 1.123))
# 1.123 is 1.12
# 取2位小数,右对齐取10位
# print("{0} is {0:>10.2f}".format(1.123))
# 1.123 is 1.12
# for i in range(1,13):
# print("|{:0>2d}|".format(i))
# print("|{:02}|".format(i))
跪求点赞和关注我!!!