Sti*_*tig 1 python string text colorama
我目前正在用Python制作游戏.
我想要代码阅读:
[00:00:00] Name|Hello!
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
print(Fore.YELLOW + Style.BRIGHT + '['),
print strftime("%H:%M:%S"),
print ']',
print(Style.BRIGHT + Fore.RED + ' Name'),
print(Fore.BLACK + '|'),
print(Fore.WHITE + Style.DIM + 'Hello!')
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)
相反 - 出于某种原因 - 它变成这样:
[ 00:00:00 ] Name | Hello!
Run Code Online (Sandbox Code Playgroud)
我不知道这段代码有什么问题,或者如何修复它.
我非常感谢能得到的所有帮助!谢谢.
使用单个print语句和逗号进行打印始终会打印尾随空格.
使用一个连接所有内容的print语句,或者使用sys.stdout.write()直接写入终端而不需要额外的空格:
print Fore.YELLOW + Style.BRIGHT + '[' + strftime("%H:%M:%S") + ']',
Run Code Online (Sandbox Code Playgroud)
要么
sys.stdout.write(Fore.YELLOW + Style.BRIGHT + '[')
sys.stdout.write(strftime("%H:%M:%S"))
sys.stdout.write(']')
Run Code Online (Sandbox Code Playgroud)
或使用字符串格式:
print '{Fore.YELLOW}{Style.BRIGHT}[{time}] {Style.BRIGHT}{Fore.RED} Name {Fore.BLACK}| {Fore.WHITE}{Style.DIM}Hello!'.format(
Style=Style, Fore=Fore, time=strftime("%H:%M:%S"))
Run Code Online (Sandbox Code Playgroud)