157

In Python say you have

s = "string"
i = 0
print s + i

will give you error, so you write

print s + str(i)

to not get error.

I think this is quite a clumsy way to handle int and string concatenation.

Even Java does not need explicit casting to String to do this sort of concatenation. Is there a better way to do this sort of concatenation, i.e, without explicit casting in Python?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
specialscope
  • 4,188
  • 3
  • 24
  • 22
  • 9
    It's because Python is Strong typed (https://en.wikipedia.org/wiki/Strong_typing) language (something to google on to understand more :)). – mouad Jul 19 '12 at 10:44
  • 4
    It would lead to ambiguity. What would be the result of `"1" + 1`, `2` or `"11"` and why? – Buddy Jul 19 '12 at 11:02
  • 1
    Buddy I am not sure what you are thinking but please dont just assume. My problem was to concatenate 2 different object types and I found the solution. I dont know how much have you programmed in python but have you ever tried to concatenate two strings only to get error when one of them is None type? You can do explicit checks ya but that is just extra bit of code. This sort of thing is required when you need to log errors. – specialscope Sep 19 '12 at 04:48
  • 3
    Just `s+str(i)`, and you're correct! –  Jan 13 '19 at 05:15

9 Answers9

196

Modern string formatting:

"{} and {}".format("string", 1)
  • 11
    Just for posterity, since things have changed in the past and are likely to change in the future: this is "modern" as of which version? – jvriesem Feb 09 '18 at 23:08
  • 2
    The `str.format` method was [introduced in Python 3.0, and backported to Python 2.6](https://docs.python.org/3.0/whatsnew/2.6.html#pep-3101-advanced-string-formatting) and later (see https://stackoverflow.com/a/792745/4648642) – MechtEngineer Apr 18 '18 at 02:37
92

No string formatting:

>> print 'Foo',0
Foo 0
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 5
    This will work well with print but cannot be assigned to a variable as a string. It gives Set object. – specialscope Jul 19 '12 at 10:49
  • 4
    It also sticks in an extra blank between the two items, unless you use the v3 `print()` *function* and set some of its parameters. – Levon Jul 19 '12 at 10:59
40

String formatting, using the new-style .format() method (with the defaults .format() provides):

 '{}{}'.format(s, i)

Or the older, but "still sticking around", %-formatting:

 '%s%d' %(s, i)

In both examples above there's no space between the two items concatenated. If space is needed, it can simply be added in the format strings.

These provide a lot of control and flexibility about how to concatenate items, the space between them etc. For details about format specifications see this.

Levon
  • 138,105
  • 33
  • 200
  • 191
21

Python is an interesting language in that while there is usually one (or two) "obvious" ways to accomplish any given task, flexibility still exists.

s = "string"
i = 0

print (s + repr(i))

The above code snippet is written in Python 3 syntax, but the parentheses after print were always allowed (optional) until version 3 made them mandatory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CaitlinG
  • 1,955
  • 3
  • 25
  • 35
9

In Python 3.6 and newer, you can format it just like this:

new_string = f'{s} {i}'
print(new_string)

Or just:

print(f'{s} {i}')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kasra Najafi
  • 560
  • 5
  • 11
6

The format() method can be used to concatenate a string and an integer:

print(s + "{}".format(i))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aakash Wadhwa
  • 91
  • 1
  • 2
  • 11
2

You can use the an f-string too!

s = "string"
i = 95
print(f"{s}{i}")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Will
  • 21
  • 3
0

Let's assume you want to concatenate a string and an integer in a situation like this:

for i in range(1, 11):
   string = "string" + i

And you are getting a type or concatenation error.

The best way to go about it is to do something like this:

for i in range(1, 11):
   print("string", i)

This will give you concatenated results, like string 1, string 2, string 3, etc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Babatunde Mustapha
  • 2,131
  • 20
  • 21
-1

If you only want to print, you can do this:

print(s, i)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131