(Python 3.x) How do I add a space before the first variable in a print function?
I am making a Tic Tac Toe program in Python 3.4.1.
I have code as follows:
def boardDraw():
board = 0,0,0,
0,0,0,
0,0,0
print(board[0] , "|" , board[1] , "|" , board[2],
"n----------n" ,
board[3] , "|" , board[4] , "|" , board[5],
"n----------n" ,
board[6] , "|" , board[7] , "|" , board[8])
boardDraw()
Output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Desired output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Is the only way to have my desired output is inserting the following:
print(end = " ")
In between board and my current print statement? I would like to have it within one print, if possible.
python python-3.x
add a comment |
I am making a Tic Tac Toe program in Python 3.4.1.
I have code as follows:
def boardDraw():
board = 0,0,0,
0,0,0,
0,0,0
print(board[0] , "|" , board[1] , "|" , board[2],
"n----------n" ,
board[3] , "|" , board[4] , "|" , board[5],
"n----------n" ,
board[6] , "|" , board[7] , "|" , board[8])
boardDraw()
Output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Desired output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Is the only way to have my desired output is inserting the following:
print(end = " ")
In between board and my current print statement? I would like to have it within one print, if possible.
python python-3.x
1
A few minutes reading up on the format function will help quite a bit
– wnnmaw
Sep 5 '14 at 20:38
From the docs: "All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end." Try str.format().
– Celeo
Sep 5 '14 at 20:39
4
Cheating:print("", board[0], ..)
. But it would be better (and easier to deal with later) if not relying on this separator auto-insertion.
– user2864740
Sep 5 '14 at 20:42
add a comment |
I am making a Tic Tac Toe program in Python 3.4.1.
I have code as follows:
def boardDraw():
board = 0,0,0,
0,0,0,
0,0,0
print(board[0] , "|" , board[1] , "|" , board[2],
"n----------n" ,
board[3] , "|" , board[4] , "|" , board[5],
"n----------n" ,
board[6] , "|" , board[7] , "|" , board[8])
boardDraw()
Output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Desired output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Is the only way to have my desired output is inserting the following:
print(end = " ")
In between board and my current print statement? I would like to have it within one print, if possible.
python python-3.x
I am making a Tic Tac Toe program in Python 3.4.1.
I have code as follows:
def boardDraw():
board = 0,0,0,
0,0,0,
0,0,0
print(board[0] , "|" , board[1] , "|" , board[2],
"n----------n" ,
board[3] , "|" , board[4] , "|" , board[5],
"n----------n" ,
board[6] , "|" , board[7] , "|" , board[8])
boardDraw()
Output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Desired output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Is the only way to have my desired output is inserting the following:
print(end = " ")
In between board and my current print statement? I would like to have it within one print, if possible.
python python-3.x
python python-3.x
asked Sep 5 '14 at 20:36


xdflamesxdflames
595
595
1
A few minutes reading up on the format function will help quite a bit
– wnnmaw
Sep 5 '14 at 20:38
From the docs: "All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end." Try str.format().
– Celeo
Sep 5 '14 at 20:39
4
Cheating:print("", board[0], ..)
. But it would be better (and easier to deal with later) if not relying on this separator auto-insertion.
– user2864740
Sep 5 '14 at 20:42
add a comment |
1
A few minutes reading up on the format function will help quite a bit
– wnnmaw
Sep 5 '14 at 20:38
From the docs: "All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end." Try str.format().
– Celeo
Sep 5 '14 at 20:39
4
Cheating:print("", board[0], ..)
. But it would be better (and easier to deal with later) if not relying on this separator auto-insertion.
– user2864740
Sep 5 '14 at 20:42
1
1
A few minutes reading up on the format function will help quite a bit
– wnnmaw
Sep 5 '14 at 20:38
A few minutes reading up on the format function will help quite a bit
– wnnmaw
Sep 5 '14 at 20:38
From the docs: "All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end." Try str.format().
– Celeo
Sep 5 '14 at 20:39
From the docs: "All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end." Try str.format().
– Celeo
Sep 5 '14 at 20:39
4
4
Cheating:
print("", board[0], ..)
. But it would be better (and easier to deal with later) if not relying on this separator auto-insertion.– user2864740
Sep 5 '14 at 20:42
Cheating:
print("", board[0], ..)
. But it would be better (and easier to deal with later) if not relying on this separator auto-insertion.– user2864740
Sep 5 '14 at 20:42
add a comment |
3 Answers
3
active
oldest
votes
Why not use .format
?
print ('{0:^3}|{1:^3}|{2:^3}'.format(*board))
Here the specifier: ^3
means to make a field of width 3 and center the text if possible.
add a comment |
There is the quick and dirty method that user2864740 provided that will solve the problem. It was my first thought when I saw the code as it was:
print(board[0] , "|" , board[1] , "|" , board[2],
to:
print("", board[0] , "|" , board[1] , "|" , board[2],
I really don't recommend this way of formatting it becomes very hard to read. This can be made a bit easier to read and maintain. You can use Python's formatting and then provide the data as parameters using %
.
So I'd use something like:
boardsep = "-" * 10
boardline = "%s | %s | %sn%s"
print(boardline % (board[0], board[1], board[2], boardsep))
print(boardline % (board[3], board[4], board[5], boardsep))
print(boardline % (board[6], board[7], board[8], boardsep))
The boardsep
is just a convenient way of taking what's in the string and duplicating it a number of times (in this case 10 times). Since the way you print the boardline
out is the same for each line I would assign it to a variable so it can be reused. You can read these print formatting docs to get a better understanding of how the parameters and format string work together.
mgilson also proposed a good solution (I upvoted it) and had me look at the OP's question again. It was for Python3 there are things you can do such as the new format method on strings, slicing and expansion. The
boardsep = '-' * 10
boardline = '{0:^3}|{1:^3}|{2:^3}n{sep}''
print (boardline.format(*board[0:3], sep=boardsep))
print (boardline.format(*board[3:6], sep=boardsep))
print (boardline.format(*board[6:9], sep=boardsep))
But you can go one further and reduce it to one complex line. If you get a thorough understanding of the basics above one could try this:
print ((('{:^3}|{:^3}|{:^3}n'+('-'*10)+'n') * 3).format(*board))
If you were to print out the expanded format specifier that generates the board it would look like this:
{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n
Since the OP did notice the problems in the output I will provide one last edit for code that is a bit more dynamic and could be put into an expanded function to generate the boards.
linesepchar = '-'
colsepchar = '|'
numrows = 3
numcols = 3
fieldwidth = 3
linesep = 'n{linesepchar:{linesepchar}^{linewidth}}n'
fieldspec = '{:^{fieldwidth}}'
lineformat = (fieldspec+'{colsepchar}')*(numcols-1)+fieldspec
boardstr = (((lineformat+linesep)*(numrows-1)+lineformat).format(
*board,linesepchar=linesepchar, colsepchar=colsepchar,
fieldwidth=fieldwidth, linewidth=((fieldwidth+1)*numcols-1)))
Only problem with your ending selection is it causes the first line to have two spaces, and an extra set of '-'. Thanks for your answer, I'll be doing a large amount of looking into for formatting.
– xdflames
Sep 5 '14 at 23:36
1
Yes you are quite correct. It does add an extra line. I was going to stop with the editing because it gives a general idea. However since you pointed out the discrepancy I did have some code I wrote earlier that almost produces nearly similar (but not quite) output. In a way I think the difference is a minor code improvement. I'll add that code as my last edit. It also wallows you to specify the rows and columns, field and col specifiers etc.
– Michael Petch
Sep 5 '14 at 23:58
add a comment |
Here if you want is how I did it before to display the tic-tac-toe board:
def __init__(self):
self.game_list = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
def print_game_board(self):
for list in self.game_list:
print(" | ".join(list))
print("--------")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25693649%2fpython-3-x-how-do-i-add-a-space-before-the-first-variable-in-a-print-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why not use .format
?
print ('{0:^3}|{1:^3}|{2:^3}'.format(*board))
Here the specifier: ^3
means to make a field of width 3 and center the text if possible.
add a comment |
Why not use .format
?
print ('{0:^3}|{1:^3}|{2:^3}'.format(*board))
Here the specifier: ^3
means to make a field of width 3 and center the text if possible.
add a comment |
Why not use .format
?
print ('{0:^3}|{1:^3}|{2:^3}'.format(*board))
Here the specifier: ^3
means to make a field of width 3 and center the text if possible.
Why not use .format
?
print ('{0:^3}|{1:^3}|{2:^3}'.format(*board))
Here the specifier: ^3
means to make a field of width 3 and center the text if possible.
answered Sep 5 '14 at 21:04


mgilsonmgilson
211k39415529
211k39415529
add a comment |
add a comment |
There is the quick and dirty method that user2864740 provided that will solve the problem. It was my first thought when I saw the code as it was:
print(board[0] , "|" , board[1] , "|" , board[2],
to:
print("", board[0] , "|" , board[1] , "|" , board[2],
I really don't recommend this way of formatting it becomes very hard to read. This can be made a bit easier to read and maintain. You can use Python's formatting and then provide the data as parameters using %
.
So I'd use something like:
boardsep = "-" * 10
boardline = "%s | %s | %sn%s"
print(boardline % (board[0], board[1], board[2], boardsep))
print(boardline % (board[3], board[4], board[5], boardsep))
print(boardline % (board[6], board[7], board[8], boardsep))
The boardsep
is just a convenient way of taking what's in the string and duplicating it a number of times (in this case 10 times). Since the way you print the boardline
out is the same for each line I would assign it to a variable so it can be reused. You can read these print formatting docs to get a better understanding of how the parameters and format string work together.
mgilson also proposed a good solution (I upvoted it) and had me look at the OP's question again. It was for Python3 there are things you can do such as the new format method on strings, slicing and expansion. The
boardsep = '-' * 10
boardline = '{0:^3}|{1:^3}|{2:^3}n{sep}''
print (boardline.format(*board[0:3], sep=boardsep))
print (boardline.format(*board[3:6], sep=boardsep))
print (boardline.format(*board[6:9], sep=boardsep))
But you can go one further and reduce it to one complex line. If you get a thorough understanding of the basics above one could try this:
print ((('{:^3}|{:^3}|{:^3}n'+('-'*10)+'n') * 3).format(*board))
If you were to print out the expanded format specifier that generates the board it would look like this:
{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n
Since the OP did notice the problems in the output I will provide one last edit for code that is a bit more dynamic and could be put into an expanded function to generate the boards.
linesepchar = '-'
colsepchar = '|'
numrows = 3
numcols = 3
fieldwidth = 3
linesep = 'n{linesepchar:{linesepchar}^{linewidth}}n'
fieldspec = '{:^{fieldwidth}}'
lineformat = (fieldspec+'{colsepchar}')*(numcols-1)+fieldspec
boardstr = (((lineformat+linesep)*(numrows-1)+lineformat).format(
*board,linesepchar=linesepchar, colsepchar=colsepchar,
fieldwidth=fieldwidth, linewidth=((fieldwidth+1)*numcols-1)))
Only problem with your ending selection is it causes the first line to have two spaces, and an extra set of '-'. Thanks for your answer, I'll be doing a large amount of looking into for formatting.
– xdflames
Sep 5 '14 at 23:36
1
Yes you are quite correct. It does add an extra line. I was going to stop with the editing because it gives a general idea. However since you pointed out the discrepancy I did have some code I wrote earlier that almost produces nearly similar (but not quite) output. In a way I think the difference is a minor code improvement. I'll add that code as my last edit. It also wallows you to specify the rows and columns, field and col specifiers etc.
– Michael Petch
Sep 5 '14 at 23:58
add a comment |
There is the quick and dirty method that user2864740 provided that will solve the problem. It was my first thought when I saw the code as it was:
print(board[0] , "|" , board[1] , "|" , board[2],
to:
print("", board[0] , "|" , board[1] , "|" , board[2],
I really don't recommend this way of formatting it becomes very hard to read. This can be made a bit easier to read and maintain. You can use Python's formatting and then provide the data as parameters using %
.
So I'd use something like:
boardsep = "-" * 10
boardline = "%s | %s | %sn%s"
print(boardline % (board[0], board[1], board[2], boardsep))
print(boardline % (board[3], board[4], board[5], boardsep))
print(boardline % (board[6], board[7], board[8], boardsep))
The boardsep
is just a convenient way of taking what's in the string and duplicating it a number of times (in this case 10 times). Since the way you print the boardline
out is the same for each line I would assign it to a variable so it can be reused. You can read these print formatting docs to get a better understanding of how the parameters and format string work together.
mgilson also proposed a good solution (I upvoted it) and had me look at the OP's question again. It was for Python3 there are things you can do such as the new format method on strings, slicing and expansion. The
boardsep = '-' * 10
boardline = '{0:^3}|{1:^3}|{2:^3}n{sep}''
print (boardline.format(*board[0:3], sep=boardsep))
print (boardline.format(*board[3:6], sep=boardsep))
print (boardline.format(*board[6:9], sep=boardsep))
But you can go one further and reduce it to one complex line. If you get a thorough understanding of the basics above one could try this:
print ((('{:^3}|{:^3}|{:^3}n'+('-'*10)+'n') * 3).format(*board))
If you were to print out the expanded format specifier that generates the board it would look like this:
{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n
Since the OP did notice the problems in the output I will provide one last edit for code that is a bit more dynamic and could be put into an expanded function to generate the boards.
linesepchar = '-'
colsepchar = '|'
numrows = 3
numcols = 3
fieldwidth = 3
linesep = 'n{linesepchar:{linesepchar}^{linewidth}}n'
fieldspec = '{:^{fieldwidth}}'
lineformat = (fieldspec+'{colsepchar}')*(numcols-1)+fieldspec
boardstr = (((lineformat+linesep)*(numrows-1)+lineformat).format(
*board,linesepchar=linesepchar, colsepchar=colsepchar,
fieldwidth=fieldwidth, linewidth=((fieldwidth+1)*numcols-1)))
Only problem with your ending selection is it causes the first line to have two spaces, and an extra set of '-'. Thanks for your answer, I'll be doing a large amount of looking into for formatting.
– xdflames
Sep 5 '14 at 23:36
1
Yes you are quite correct. It does add an extra line. I was going to stop with the editing because it gives a general idea. However since you pointed out the discrepancy I did have some code I wrote earlier that almost produces nearly similar (but not quite) output. In a way I think the difference is a minor code improvement. I'll add that code as my last edit. It also wallows you to specify the rows and columns, field and col specifiers etc.
– Michael Petch
Sep 5 '14 at 23:58
add a comment |
There is the quick and dirty method that user2864740 provided that will solve the problem. It was my first thought when I saw the code as it was:
print(board[0] , "|" , board[1] , "|" , board[2],
to:
print("", board[0] , "|" , board[1] , "|" , board[2],
I really don't recommend this way of formatting it becomes very hard to read. This can be made a bit easier to read and maintain. You can use Python's formatting and then provide the data as parameters using %
.
So I'd use something like:
boardsep = "-" * 10
boardline = "%s | %s | %sn%s"
print(boardline % (board[0], board[1], board[2], boardsep))
print(boardline % (board[3], board[4], board[5], boardsep))
print(boardline % (board[6], board[7], board[8], boardsep))
The boardsep
is just a convenient way of taking what's in the string and duplicating it a number of times (in this case 10 times). Since the way you print the boardline
out is the same for each line I would assign it to a variable so it can be reused. You can read these print formatting docs to get a better understanding of how the parameters and format string work together.
mgilson also proposed a good solution (I upvoted it) and had me look at the OP's question again. It was for Python3 there are things you can do such as the new format method on strings, slicing and expansion. The
boardsep = '-' * 10
boardline = '{0:^3}|{1:^3}|{2:^3}n{sep}''
print (boardline.format(*board[0:3], sep=boardsep))
print (boardline.format(*board[3:6], sep=boardsep))
print (boardline.format(*board[6:9], sep=boardsep))
But you can go one further and reduce it to one complex line. If you get a thorough understanding of the basics above one could try this:
print ((('{:^3}|{:^3}|{:^3}n'+('-'*10)+'n') * 3).format(*board))
If you were to print out the expanded format specifier that generates the board it would look like this:
{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n
Since the OP did notice the problems in the output I will provide one last edit for code that is a bit more dynamic and could be put into an expanded function to generate the boards.
linesepchar = '-'
colsepchar = '|'
numrows = 3
numcols = 3
fieldwidth = 3
linesep = 'n{linesepchar:{linesepchar}^{linewidth}}n'
fieldspec = '{:^{fieldwidth}}'
lineformat = (fieldspec+'{colsepchar}')*(numcols-1)+fieldspec
boardstr = (((lineformat+linesep)*(numrows-1)+lineformat).format(
*board,linesepchar=linesepchar, colsepchar=colsepchar,
fieldwidth=fieldwidth, linewidth=((fieldwidth+1)*numcols-1)))
There is the quick and dirty method that user2864740 provided that will solve the problem. It was my first thought when I saw the code as it was:
print(board[0] , "|" , board[1] , "|" , board[2],
to:
print("", board[0] , "|" , board[1] , "|" , board[2],
I really don't recommend this way of formatting it becomes very hard to read. This can be made a bit easier to read and maintain. You can use Python's formatting and then provide the data as parameters using %
.
So I'd use something like:
boardsep = "-" * 10
boardline = "%s | %s | %sn%s"
print(boardline % (board[0], board[1], board[2], boardsep))
print(boardline % (board[3], board[4], board[5], boardsep))
print(boardline % (board[6], board[7], board[8], boardsep))
The boardsep
is just a convenient way of taking what's in the string and duplicating it a number of times (in this case 10 times). Since the way you print the boardline
out is the same for each line I would assign it to a variable so it can be reused. You can read these print formatting docs to get a better understanding of how the parameters and format string work together.
mgilson also proposed a good solution (I upvoted it) and had me look at the OP's question again. It was for Python3 there are things you can do such as the new format method on strings, slicing and expansion. The
boardsep = '-' * 10
boardline = '{0:^3}|{1:^3}|{2:^3}n{sep}''
print (boardline.format(*board[0:3], sep=boardsep))
print (boardline.format(*board[3:6], sep=boardsep))
print (boardline.format(*board[6:9], sep=boardsep))
But you can go one further and reduce it to one complex line. If you get a thorough understanding of the basics above one could try this:
print ((('{:^3}|{:^3}|{:^3}n'+('-'*10)+'n') * 3).format(*board))
If you were to print out the expanded format specifier that generates the board it would look like this:
{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n{:^3}|{:^3}|{:^3}n----------n
Since the OP did notice the problems in the output I will provide one last edit for code that is a bit more dynamic and could be put into an expanded function to generate the boards.
linesepchar = '-'
colsepchar = '|'
numrows = 3
numcols = 3
fieldwidth = 3
linesep = 'n{linesepchar:{linesepchar}^{linewidth}}n'
fieldspec = '{:^{fieldwidth}}'
lineformat = (fieldspec+'{colsepchar}')*(numcols-1)+fieldspec
boardstr = (((lineformat+linesep)*(numrows-1)+lineformat).format(
*board,linesepchar=linesepchar, colsepchar=colsepchar,
fieldwidth=fieldwidth, linewidth=((fieldwidth+1)*numcols-1)))
edited Sep 6 '14 at 0:07
answered Sep 5 '14 at 21:02
Michael PetchMichael Petch
26k556102
26k556102
Only problem with your ending selection is it causes the first line to have two spaces, and an extra set of '-'. Thanks for your answer, I'll be doing a large amount of looking into for formatting.
– xdflames
Sep 5 '14 at 23:36
1
Yes you are quite correct. It does add an extra line. I was going to stop with the editing because it gives a general idea. However since you pointed out the discrepancy I did have some code I wrote earlier that almost produces nearly similar (but not quite) output. In a way I think the difference is a minor code improvement. I'll add that code as my last edit. It also wallows you to specify the rows and columns, field and col specifiers etc.
– Michael Petch
Sep 5 '14 at 23:58
add a comment |
Only problem with your ending selection is it causes the first line to have two spaces, and an extra set of '-'. Thanks for your answer, I'll be doing a large amount of looking into for formatting.
– xdflames
Sep 5 '14 at 23:36
1
Yes you are quite correct. It does add an extra line. I was going to stop with the editing because it gives a general idea. However since you pointed out the discrepancy I did have some code I wrote earlier that almost produces nearly similar (but not quite) output. In a way I think the difference is a minor code improvement. I'll add that code as my last edit. It also wallows you to specify the rows and columns, field and col specifiers etc.
– Michael Petch
Sep 5 '14 at 23:58
Only problem with your ending selection is it causes the first line to have two spaces, and an extra set of '-'. Thanks for your answer, I'll be doing a large amount of looking into for formatting.
– xdflames
Sep 5 '14 at 23:36
Only problem with your ending selection is it causes the first line to have two spaces, and an extra set of '-'. Thanks for your answer, I'll be doing a large amount of looking into for formatting.
– xdflames
Sep 5 '14 at 23:36
1
1
Yes you are quite correct. It does add an extra line. I was going to stop with the editing because it gives a general idea. However since you pointed out the discrepancy I did have some code I wrote earlier that almost produces nearly similar (but not quite) output. In a way I think the difference is a minor code improvement. I'll add that code as my last edit. It also wallows you to specify the rows and columns, field and col specifiers etc.
– Michael Petch
Sep 5 '14 at 23:58
Yes you are quite correct. It does add an extra line. I was going to stop with the editing because it gives a general idea. However since you pointed out the discrepancy I did have some code I wrote earlier that almost produces nearly similar (but not quite) output. In a way I think the difference is a minor code improvement. I'll add that code as my last edit. It also wallows you to specify the rows and columns, field and col specifiers etc.
– Michael Petch
Sep 5 '14 at 23:58
add a comment |
Here if you want is how I did it before to display the tic-tac-toe board:
def __init__(self):
self.game_list = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
def print_game_board(self):
for list in self.game_list:
print(" | ".join(list))
print("--------")
add a comment |
Here if you want is how I did it before to display the tic-tac-toe board:
def __init__(self):
self.game_list = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
def print_game_board(self):
for list in self.game_list:
print(" | ".join(list))
print("--------")
add a comment |
Here if you want is how I did it before to display the tic-tac-toe board:
def __init__(self):
self.game_list = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
def print_game_board(self):
for list in self.game_list:
print(" | ".join(list))
print("--------")
Here if you want is how I did it before to display the tic-tac-toe board:
def __init__(self):
self.game_list = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
def print_game_board(self):
for list in self.game_list:
print(" | ".join(list))
print("--------")
answered Nov 22 '18 at 10:21
Nathan AddaNathan Adda
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25693649%2fpython-3-x-how-do-i-add-a-space-before-the-first-variable-in-a-print-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
A few minutes reading up on the format function will help quite a bit
– wnnmaw
Sep 5 '14 at 20:38
From the docs: "All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end." Try str.format().
– Celeo
Sep 5 '14 at 20:39
4
Cheating:
print("", board[0], ..)
. But it would be better (and easier to deal with later) if not relying on this separator auto-insertion.– user2864740
Sep 5 '14 at 20:42