How do I append one string to another in Python

0 votes
I want an efficient way to append one string to another in Python.

var1 = "foo"
var2 = "bar"
var3 = var1 + var2
 

Is there any good built-in method to use?
Oct 22, 2018 in Python by ana1504.k
• 7,910 points
739 views

1 answer to this question.

0 votes
If you only have one reference to a string and you concatenate another string to the end, CPython now special cases this and tries to extend the string in place.

The end result is that the operation is amortized O(n).

e.g.

s = ""
for i in range(n):
    s+=str(i)
used to be O(n^2), but now it is O(n).
answered Oct 22, 2018 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer

How do I make one turtle follow another turtle in python?

Try something like this: from turtle import Turtle, ...READ MORE

answered Jul 8, 2019 in Python by Ayushi
5,354 views
0 votes
1 answer

How do I check if a given Python string is a substring of another one?

Try using in like this: >>> x = 'hello' >>> y ...READ MORE

answered Nov 26, 2020 in Python by Gitika
• 65,770 points
749 views
0 votes
0 answers

i want to ask that how can i run one file of python in another file in jupyter notebook

motion_detection.ipynb # Python program to implement # Webcam Motion ...READ MORE

Dec 16, 2020 in Python by Ramsha
• 120 points
840 views
0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,270 points
1,886 views
0 votes
2 answers
0 votes
1 answer

splitting and concatenating a string

You can try the following code in ...READ MORE

answered Apr 17, 2019 in Python by SDeb
• 13,300 points
949 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer

In Python, how do I read a file line-by-line into a list?

with open(fname) as f:     content = f.readlines() # you ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 13,300 points
1,792 views
0 votes
1 answer

How do I convert a Python program to a runnable .exe Windows program?

Understand that every 'freezing' application for Python ...READ MORE

answered Oct 13, 2018 in Python by SDeb
• 13,300 points
1,458 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP