Concatenating string and integer in Python

PythonConcatenationString Concatenation

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

Modern string formatting:

"{} and {}".format("string", 1)

Solution 2 - Python

No string formatting:

>> print 'Foo',0
Foo 0

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

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

print(s + "{}".format(i))

Solution 6 - Python

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}')

Solution 7 - Python

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

print(s, i)

Solution 8 - Python

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.

Solution 9 - Python

You can use the an f-string too!

s = "string"
i = 95
print(f"{s}{i}")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspecialscopeView Question on Stackoverflow
Solution 1 - Pythonuser647772View Answer on Stackoverflow
Solution 2 - PythonBurhan KhalidView Answer on Stackoverflow
Solution 3 - PythonLevonView Answer on Stackoverflow
Solution 4 - PythonCaitlinGView Answer on Stackoverflow
Solution 5 - PythonAakash WadhwaView Answer on Stackoverflow
Solution 6 - PythonKasra NajafiView Answer on Stackoverflow
Solution 7 - PythonDaniel JiménezView Answer on Stackoverflow
Solution 8 - PythonMustapha BabatundeView Answer on Stackoverflow
Solution 9 - PythonWillView Answer on Stackoverflow