0

I would like to use a variable as an integer and a string in the same statement. In BASIC, it would look like this:

cnt = 0

while cnt < 10:
    cnt = cnt + 1
    print cnt + " + 1 = " + cnt + 1 

I understand it's a bit different with Python (integers have to be converted before you print them as strings, but here using a function such as "cnt = str(cnt)" doesn't seem to work very well). What would be the easy way to do this?

1
  • 1
    When you say "BASIC", which variant do you mean? There are a lot of different ones. Commented Aug 7, 2011 at 1:55

6 Answers 6

6

There isn't one, since Python is strongly typed.

print '%d + 1 = %d' % (cnt, cnt + 1)
Sign up to request clarification or add additional context in comments.

4 Comments

Use of the % operator in this way is discouraged (possibly deprecated). Users of newer versions of Python (2.6+) should favour the format method.
It, however, is what people are used to and continues to work through 3.2+.
@Marcelo, I don't think it's deprecated yet. .format() doesn't work on Python <2.6 (including google app engine). I agree that it is preferred, but I'd still use format strings if the version isn't specified as many people are still using 2.5 and even 2.4
@agf: I agree that's what they're used to (and indeed must use if they are on older versions), but we should still encourage use of modern idioms where practicable.
5

For Python 2.6+, the format method is the preferred way to build up strings containing other values:

'{0} + 1 = {1}'.format(cnt, cnt + 1)

1 Comment

+1, but you should mention that Python2.6+ is required for this answer
3

You had it nearly right; you just need to tell Python exactly when to make something a string.

>>> cnt = 0
>>> while cnt < 10:
...     cnt += 1
...     print str(cnt) + " + 1 = " + str(cnt + 1)
... 
1 + 1 = 2
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
5 + 1 = 6
6 + 1 = 7
7 + 1 = 8
8 + 1 = 9
9 + 1 = 10
10 + 1 = 11

Strictly speaking, you aren't treating cnt as both an integer and a string here -- it's always an integer.

Comments

1
cnt = 0

while cnt < 10:
    print str(cnt) + " + 1 = " + str(cnt + 1)
    cnt = cnt + 1

or

for cnt in range(10):
    print str(cnt) + " + 1 = " + str(cnt + 1)

or

for cnt in range(10):
    print "%d + 1 = %d".format(cnt, cnt + 1)

Comments

1
print str(cnt) + " + 1 = " + str(cnt + 1)

or

print "%d + 1 = %d" % (cnt, cnt+1)

Comments

1

You can print ints as well as strings. You may be confused by some of the answers if you know that it is quite possible to write print cnt on its own, without explicitly putting it into a string.

The string formatting language is useful and powerful and something you do need to know about. But it's perfectly fine to write

print cnt, "+ 1 =", cnt + 1

which is closer in spirit to what you asked. Python automatically adds spaces between the arguments, and a newline character at the end of the line (to suppress this, add another comma at the end).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.