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'

1

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

4 Comments

And I always thought string concatenation with + sign was slow. Maybe because the strings are too short?
Maybe, I've uploaded my script to Ideone, where the results are even more pronounced: ideone.com/a6JMi
I did not know .format method was slower than % method of formatting. I thought .format was the preferred method. Is it always slower?
Well for everything up to 500 character strings, yes. You can tweak my script to run further tests if you want.
2

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

3 Comments

Since it's explicitly tagged python-3.x I'd remove that second option.
@fredley, I read % would not be removed anytime soon. In the new python 3.2 it stays.
For Python 3, you can use String3 = "{0}{1}".format(String1, String2).
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.

Comments

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)

Comments

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'

Comments

-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.

1 Comment

What do you mean?

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.