我不明白为什么用\t在代码之间输入一个空格,在“绿色”和“到目前为止我学到的一些东西:”输出之间留出一行空间。当我使用\n时,它在中间有两个空格。空间不应该是相同的\t和\n吗?我知道\t做制表符\n是新行。但我不明白代码之间的两个空格是如何做到的:
fav_num = {
'rachel':'blue',
'hannah':'green',
}
print(fav_num['rachel'])
print(fav_num['hannah'])
#6-3
coding_glossary = {
'list':'mutable type where you can store info',
'tuple':'immutable type similar to list',
'string':'simple line of code'
}
print('\t')
print('Some things I learned so far: \n')
print('What a list is:')
print(coding_glossary['list'])产出如下:
blue
green
Some things I learned so far:
What a list is:
mutable type where you can store info
Process finished with exit code 0发布于 2020-06-15 20:56:16
python的内置打印功能隐式地将'\n‘作为结束字符。
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False): 将对象打印到文本流文件中,由sep和分隔,然后是end。9、结束、文件和刷新(如果存在)必须作为关键字参数给出。
因此,每次运行print()时,都会有一个“\n”字符被隐式打印,除非您通过将end=传递给它来覆盖该行为。(比如end='' )
发布于 2020-06-15 20:56:02
您的代码可以等效地编写:
#
print()
print(‘Some things I learned so far:’)
print()
#发布于 2020-06-15 20:56:57
默认情况下,print将新行放在末尾,若要修改此行为,可以使用end=设置end参数“”。
示例:
print("this will use 2 lines \n")
print("this will use 1 line")
print("this will use 1 line \n", end="")https://stackoverflow.com/questions/62396765
复制相似问题