0

I am newbie with Python formatting.I have assigned an integer to a variable k as below:

k = 1
 tel = tel + [(%d + 1) % k]
print tel

I want to concatenate the string tel with the sum of %d +1

The above code does not work as I am sure there is something wrong in the syntax. How do I write the correct form

Thanks!

Avinash Babu
  • 6,171
  • 3
  • 21
  • 26

3 Answers3

0

Seems like what you want is str() function in python.which coerces data into a string

k = 1
 tel = tel + str(k+1)
print tel
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
0

If tel is a string :

k = 1
tel = "tel%i1" % k
# or 
# tel = "tel" + str(k) +"1"
print tel
tel11

If it's a variable :

tel = "foo"
k = 1
tel = "%s%i1" %(tel, k)
# or
# tel = tel + str(k) + "1"
print tel
foo11

If you want to perform addition before concatenating :

k = 1
tel = "tel%i" %(k+1)
# or
# tel = "tel" + str(k+1)
print tel
tel2

hope this helps.

jrjc
  • 21,103
  • 9
  • 64
  • 78
0

to concatenate string with integer, you should convert integer into string (str()).

print ’hello’ + str(1) etc...

hope this w