我还有别的办法写这个吗?我只知道打印功能,但我似乎不能像examp那样得到它

2024-06-16 10:50:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎无法得到与示例相同的打印函数 我已经用了基本的打印,但它不会给我什么我要找的,而且逗号似乎没有划分它

python 2.7版

print "NUCLEAR CORE UNSTABLE!!!, Quarantine is in effect. , Surrounding hamlets will be evacuated. , Anti-radiationsuits and iodine pills are mandatory."

enter image description here


Tags: incore示例isbewillprint逗号
2条回答
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.'''

您也可以使用\t来放入制表符空间

您使用的是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")

我个人比较喜欢这个。您可以将所有代码放在一行中,但这会使代码难以读取和维护

相关问题 更多 >