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
491 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
4,687 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,910 points
342 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 ...READ MORE

Dec 16, 2020 in Python by Ramsha
• 120 points
697 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,578 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
516 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,023 views
0 votes
2 answers

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

readline function help to  read line in ...READ MORE

answered Jun 21, 2020 in Python by sahil
• 580 points
1,434 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,233 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