I want to print
1 22 333 4444 ... & so on until 10 My code:
for i in range(1,11): print 'i'*i I get the output:
i ii iii iiii ... Where am I going wrong? I need the i value, I tried '%d'*i %i but I still get the wrong output.
You are duplicating (using the overloaded * operator) the string 'i' i times - instead, you should duplicate the string representation of the i variable:
for i in range(1,11): print str(i)*i You just had the slightly wrong symbol, single quotes instead of backticks. The latter work fine:
>>> for i in range(1,11): print `i`*i 1 22 333 4444 55555 666666 7777777 88888888 999999999 10101010101010101010 I assume you saw someone else use them and didn't notice the difference? I'm not sure whether they're frowned upon, I just know that Python 3 removed them.
>>> print '\n'.join(str(i)*i for i in xrange(1, 11)) 1 22 333 4444 55555 666666 7777777 88888888 999999999 10101010101010101010 This is an alternative approach using str.join with a generator expression in order to construct the entire output first as one large string and then simply print that string.
Our goal is to first construct the string so we do so by iterating through xrange(1, 11) and doing sequence multiplication on the string version of i to get the required string. In Python, when you multiply a sequence by an integer n, you get a new sequence that consists of the original sequence repeated n times. Strings are sequences in Python, so this is an easy way of repeating a string n times.
Next, we join together all the string pieces with the newline character. When we have our big string, we just print it out in a single statement.
I understand that other answer codes are less verbose, but, Just in case you 'do not' want to typecast the int to str, you may use the below code, to produce the same output
row = 1 base_length = int(input("Base Length: ")) while row <= base_length: iteration = row while iteration: print(row," ", end= '') iteration -= 1 print("\n") row += 1