5

Is there any way to add one string to the end of another in python? e.g.

String1 = 'A' String2 = 'B'

and i want String3 == 'AB'

fredley
  • 32,953
  • 42
  • 145
  • 236
Matt
  • 1,471
  • 8
  • 20
  • 28
  • Does this answer your question? [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/questions/12169839/which-is-the-preferred-way-to-concatenate-a-string-in-python) – Vega Oct 10 '20 at 10:51

6 Answers6

8

String concatenation in python is straightforward

a = "A"
b = "B"
c = a + b
print c

> AB

I benchmarked the three operations, performing 1m of each:

c = a + b
c = '%s%s' % (a,b)
c = "{0}{1}".format(a, b)

And the results are:

+:  0.232225275772
%s: 0.42436670365
{}: 0.683854960343

Even with 500 character strings, + is still fastest by far. My script is on ideone and the results (for 500 char strings) are:

+: 0.82
%s: 1.54
{}: 2.03
fredley
  • 32,953
  • 42
  • 145
  • 236
  • And I always thought string concatenation with + sign was slow. Maybe because the strings are too short? – Utku Zihnioglu Mar 08 '11 at 22:42
  • Maybe, I've uploaded my script to Ideone, where the results are even more pronounced: http://ideone.com/a6JMi – fredley Mar 08 '11 at 22:46
  • I did not know .format method was slower than % method of formatting. I thought .format was the preferred method. Is it always slower? – Curious2learn Mar 09 '11 at 02:57
  • Well for everything up to 500 character strings, yes. You can tweak my script to run further tests if you want. – fredley Mar 09 '11 at 10:00
2

You could use the simplest version: String3 = String1 + String2 or the format operator (deprecated in python3): String3 = '%s%s' % (String1, String2)

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2

In Python, the + operator concatenates strings:

>>> String3 = String1 + String2
>>> String3
   'AB'

This is the simplest way and usually it's the right choice. However, sometimes you might need a more efficient string concatenation.

snakile
  • 52,936
  • 62
  • 169
  • 241
1

For simplicity when speed doesn't matter, you can't beat the ease of c=a+b. If speed does matter (because you're making a large number of successive concatenations, for example), str.join() can be a little more efficient (code at ideone).

+: 2.51

''.join([a,...z]): 0.2

append(): 2.05

From what I can tell, if you are making successive concatenations without touching the intermediate product, I'm better off appending each addition to a list, then joining everything at once. For the single concatenation case, a+b is still faster than a.join(b)

dschafer
  • 53
  • 1
  • 6
0

You can also try out str.join():

>>>s1='a'
>>>s2='b'
>>>s3=''.join((s1,s2))
>>>s3
'ab'

also if you write:

>>>s3='WhatEver'.join((s1,s2))
>>>s3
'aWhatEverb'
hencrice
  • 167
  • 1
  • 1
  • 10
-1

Here is the code for string concatenation: `

String1 = "a"
String2 = "b"
String3 = String1 + String2
#String 3 would be ab

` You can add more than two string variables into one string variable.

Bob
  • 1
  • 7