You could do this on one line with join
and a generator expression.
def hash_print(number):
print ('\n'.join(('#' * (i + 1)).rjust(number) for i in range(number)))
A generator expression is like a for
loop collapsed into a one line expression. It will loop over range(number)
and create right justified strings of hashes, joining them together with newline characters in between them.
Also to note about some of your usages, you should put spaces between each operator, as it's easier to read. Changing this:
hashes='#' *(i+1)
to this:
hashes = '#' * (i + 1)
Also you should use str.format
, not the %
syntax for passing parameters to a string. str.format
has a lot of useful formatting and is the accepted form nowadays. %
is just older syntax. You can read about the usefulness of str.format
here.
Though in your case they're both strings so you can just use string concatenation with the +
operator:
print( spaces + hashes )