Python print()函数

print()函数的详细语法格式如下:

print(value, ..., sep='', end='\n', file=sys.stdout, flush=False)
  • value参数可以接受任意多个变量或值, 因此可以输出多个值;
  • 输出多个值时, 默认以空格隔开多个值, 如果希望改变默认的分隔府, 可以通过sep参数进行设置;

Example:

# 设置分隔符
print('hello', 'world', sep='|')
  • 默认情况下, print()函数输出之后总会换行, 这是因为print()函数的end参数的默认值是\n, 这个\n就代表了换行. 如果希望print()函数输出之后不换行, 则重设end参数即可;

Example:

# 设置输出一行后不换行
print('hello', 'world', end='')
  • file参数指定print()函数的输出目标, file参数的默认值是sys.stdout, 该值默认代表了标准系统输出, 因此print()函数默认输出到终端. 实际上, 完全可以通过改变该参数让print()输出到指定文件中;

Example:

# 指定输出到文件
f = open('example.txt', 'w')
print('hello', 'world', file=f)
f.close()
  • flush参数用于控制输出缓存, 该参数一般保持为False即可, 这样可以获得较好的性能.