使用\ n格式化Python字符串

时间:2013-11-30 04:32:44

标签: python string formatting

im trying to make a board like that with strings 我试图使用\n将此字符串格式化为电路板,某处存在语法错误,但我不知道在哪里!

f1= ("a"   "b"   "c" + "|"\n + "   -------------"+"\n" + "1  |",gameboard[0],"|",gameboard    [    1], "|" ,gameboard[2], "|"+"\n" + "2","|",gameboard[3],"|",gameboard[4], "|" ,gameboard[5],"|" +"\n" + "3","|",gameboard[6],"|",gameboard[7], "|" ,gameboard[8], "|"+"\n"+ "-------------")


gameboard = ['O', ' ', ' ', 'O', 'X', ' ', 'O', ' ', 'X']
print(f1)

1 个答案:

答案 0 :(得分:4)

你已经遇到麻烦了:

f1= ("a"   "b"   "c" + "|"\n

不幸的是,我无法猜出你希望希望做什么。它相当于:

f1= ("abc|"\n

如果你只是将\n单独放在一行上,你就会得到同样的错误:

>>> \n
SyntaxError: unexpected character after line continuation character

\n在带引号的字符串之外没有任何意义。

后来:现在你添加了一张图片,这是一个开始:

print "    a   b   c"
print "  -----------"

这很简单。它只是简单的坚果;-)试图把它全部塞进一条线上。如果你觉得必须,那两张照片就相当于这张照片 - 更难阅读 - 打印:

print "    a   b   c\n  -----------"