Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation vs. string substitution in Python

In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?

For a concrete example, how should one handle construction of flexible URIs:

DOMAIN = 'http://stackoverflow.com' QUESTIONS = '/questions'  def so_question_uri_sub(q_num):     return "%s%s/%d" % (DOMAIN, QUESTIONS, q_num)  def so_question_uri_cat(q_num):     return DOMAIN + QUESTIONS + '/' + str(q_num) 

Edit: There have also been suggestions about joining a list of strings and for using named substitution. These are variants on the central theme, which is, which way is the Right Way to do it at which time? Thanks for the responses!

like image 502
gotgenes Avatar asked Dec 17 '08 23:12

gotgenes


People also ask

What is the difference between string interpolation and string concatenation?

Concatenation allows you to combine to strings together and it only works on two strings. Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable.

What are the benefits of using the .format method instead of string concatenation in Python?

The main advantages of using format(…) are that the string can be a bit easier to produce and read as in particular in the second example, and that we don't have to explicitly convert all non-string variables to strings with str(…).

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

What is string concatenation in Python?

Concatenating means obtaining a new string that contains both of the original strings. In Python, there are a few ways to concatenate or combine strings. The new string that is created is referred to as a string object. In order to merge two strings into a single object, you may use the + operator.


2 Answers

Concatenation is (significantly) faster according to my machine. But stylistically, I'm willing to pay the price of substitution if performance is not critical. Well, and if I need formatting, there's no need to even ask the question... there's no option but to use interpolation/templating.

>>> import timeit >>> def so_q_sub(n): ...  return "%s%s/%d" % (DOMAIN, QUESTIONS, n) ... >>> so_q_sub(1000) 'http://stackoverflow.com/questions/1000' >>> def so_q_cat(n): ...  return DOMAIN + QUESTIONS + '/' + str(n) ... >>> so_q_cat(1000) 'http://stackoverflow.com/questions/1000' >>> t1 = timeit.Timer('so_q_sub(1000)','from __main__ import so_q_sub') >>> t2 = timeit.Timer('so_q_cat(1000)','from __main__ import so_q_cat') >>> t1.timeit(number=10000000) 12.166618871951641 >>> t2.timeit(number=10000000) 5.7813972166853773 >>> t1.timeit(number=1) 1.103492206766532e-05 >>> t2.timeit(number=1) 8.5206360154188587e-06  >>> def so_q_tmp(n): ...  return "{d}{q}/{n}".format(d=DOMAIN,q=QUESTIONS,n=n) ... >>> so_q_tmp(1000) 'http://stackoverflow.com/questions/1000' >>> t3= timeit.Timer('so_q_tmp(1000)','from __main__ import so_q_tmp') >>> t3.timeit(number=10000000) 14.564135316080637  >>> def so_q_join(n): ...  return ''.join([DOMAIN,QUESTIONS,'/',str(n)]) ... >>> so_q_join(1000) 'http://stackoverflow.com/questions/1000' >>> t4= timeit.Timer('so_q_join(1000)','from __main__ import so_q_join') >>> t4.timeit(number=10000000) 9.4431309007150048 
like image 89
Vinko Vrsalovic Avatar answered Sep 21 '22 19:09

Vinko Vrsalovic


Don't forget about named substitution:

def so_question_uri_namedsub(q_num):     return "%(domain)s%(questions)s/%(q_num)d" % locals() 
like image 38
too much php Avatar answered Sep 21 '22 19:09

too much php