0

So far, I have:

def printSquare(x,y):
        for z in range(0,x):
                print(x*y)

printSquare(3,'%') prints out something like this:

%%%
%%%
%%%

instead of using the '*' operation, is there a way to print x numbers of y separated by commas (for better spacing) ie: print(y,y,y)?

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Gribouillis
Featured Replies
  • [QUOTE=coril;1407775]after changing your code, I found that [CODE]print(*3*(y,))[/CODE] gives the desired result. What does the '*' before the 3 mean? when I left it out, it returned: print(3,'%') ('%','%','%')[/QUOTE] In the python console [code=python] >>> def printSquare(x,y): ... for z in range(0,x): ... print(*(y,)*x, sep=",") ... >>> printSquare(3, "%") %,%,% … Read More

0

This prints it out with the commas as though it were a string while print(y,y,y) doesn't print out the commas but interprets them as spaces.

0

after changing your code, I found that

print(*3*(y,))

gives the desired result. What does the '*' before the 3 mean? when I left it out, it returned:
print(3,'%')
('%','%','%')

1

after changing your code, I found that

print(*3*(y,))

gives the desired result. What does the '*' before the 3 mean? when I left it out, it returned:
print(3,'%')
('%','%','%')

In the python console

>>> def printSquare(x,y):
...  for z in range(0,x):
...   print(*(y,)*x, sep=",")
... 
>>> printSquare(3, "%")
%,%,%
%,%,%
%,%,%

The * is used when you pass a sequence of arguments to a function. print(*(y,y,y)) is the same as print(y,y,y) ,
but the argument list doesn't need to be written explicitly. For example

my_tuple = ('a', 'b', 'c')
print(*my_tuple) # prints a b c, identical to print('a','b','c')
print(my_tuple) # prints ('a', 'b', 'c'), identical to print(('a','b','c'))

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.