我似乎不能得到与示例相同的打印功能我已经使用了基本的打印,但它不会给我我想要的,而且逗号似乎没有分割它
python 2.7
print "NUCLEAR CORE UNSTABLE!!!, Quarantine is in effect. , Surrounding hamlets will be evacuated. , Anti-radiationsuits and iodine pills are mandatory."
发布于 2019-06-20 03:22:17
您使用的是print
语句,而不是函数,有几种方法可以做到这一点:
它使用三重引号字符串来保留换行符:
def printit():
print """NUCLEAR CORE UNSTABLE!!!
Quarantine is in effect.
Surrounding hamlets will be evacuated.
Anti-radiationsuits and iodine pills are mandatory.
"""
要运行3次,只需:
for i in range(3):
printit()
它使用了多条print
语句:
def printit():
print "NUCLEAR CORE UNSTABLE!!!"
print "Quarantine is in effect."
print "Surrounding hamlets will be evacuated."
print "Anti-radiationsuits and iodine pills are mandatory.\n"
这只使用了一行,并嵌入了换行符:
def printit():
print "NUCLEAR CORE UNSTABLE!!!\nQuarantine is in effect.\nSurrounding hamlets will be evacuated.\nAnti-radiationsuits and iodine pills are mandatory.\n"
但是,您提到了print
函数,并抱怨逗号分隔符没有做任何事情,所以:
from __future__ import print_function
def printit():
print ("NUCLEAR CORE UNSTABLE!!!",
"Quarantine is in effect.",
"Surrounding hamlets will be evacuated.",
"Anti-radiationsuits and iodine pills are mandatory.\n",
sep="\n")
就我个人而言,我更喜欢这个。您可以将所有内容放在一行中,但这会使代码难以阅读和维护。
发布于 2019-06-20 02:42:42
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
print bcolors.WARNING + "Warning: No active frommets remain. Continue?"
+ bcolors.ENDC
这是我在网上找到的一个代码片段,很好用!
print bcolors.WARNING + "NUCLEAR CORE UNSTABLE!!!" + bcolors.ENDC + '''\n Quarantine is in effect. \n
Surrounding hamlets will be evacuated. , Anti-radiationsuits and iodine pills are mandatory.'''
https://stackoverflow.com/questions/56673964
复制相似问题