1

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.

7 Answers 7

4

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Well, you are not really multiplying. Rather it is an operator overloaded asterisk, which is used to repeat a given string.
@MalikBrahimi indeed - couldn't think of a better term for it. I replaced multiplying with "multiplying", to attempt to make it clearer. Not sure I really did, though
Rephrased - I think "duplicate" is more appropriate that "multiply". Suggestions for better wording are welcome.
1

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.

Comments

0

You need to call str(i) :

for i in range(1,11): print str(i) * i 

You are actually creating a string "i", not using the str representation of the variable i.

Output:

1 22 333 4444 55555 666666 7777777 88888888 999999999 10101010101010101010 

Comments

0
In [21]: for i in range(1,11): ....: print str(i)*i 

Comments

0

You are printing the string 'i' for i times instead of str(i) for i times.

Comments

0
>>> 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.

Comments

0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.