Python string formatting: % vs concatenation

python string concatenation
python f-string
python concatenate string and variable
python string interpolation
python efficient string concatenation
python invalid syntax string concatenation
python append to string
python multi-line format string

I'm developing an application in which I perform some requests to get an object id. After each one of them, I call a method (get_actor_info()) passing this id as an argument (see code below).

ACTOR_CACHE_KEY_PREFIX = 'actor_'

def get_actor_info(actor_id):
    cache_key = ACTOR_CACHE_KEY_PREFIX + str(actor_id)

As can be noticed, I'm casting actor_id to string and concatenating it with a prefix. However, I know I could do it in multiple other ways (.format() or '%s%d', for instance) and that results in my question: would '%s%d' be better than string concatenation in terms of readability, code convention and efficiency?

Thanks

This could easily become an opinion-based thread, but I find formatting to be more readable in most cases, and more maintainable. It's easier to visualize what the final string will look like, without doing "mental concatenation". Which of these is more readable, for example?

errorString = "Exception occurred ({}) while executing '{}': {}".format(
    e.__class__.__name__, task.name, str(e)
)

Or:

errorString = "Exception occurred (" + e.__class__.__name__
    + ") while executing '" + task.name + "': " + str(e)

As for whether to use % or .format(), I can answer more objectively: Use .format(). % is the "old-style", and, per the Python Documentation they may soon be removed:

Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used.

Later versions of the documentation have stopped mentioning this, but nonetheless, .format() is the way of the future; use it!

Concatenation is faster, but that should not be a concern. Make your code readable and maintainable as a first-line goal, and then optimize the parts you need to optimize later. Premature optimization is the root of all evil ;)

Format strings vs concatenation, Python str.format versus concatenation like the format method mostly because i can insert variables exactly where i want to in a string. In this brief introduction to string formatting in Python, we will explore live examples of string concatenation, string substitution, and additional formatting methods for common string operations.

Concatenation is better when it comes to performance. In your example, both concatenation and substitution are readable but when it comes to more complex templates, substitution wins the simplicity and readability race.

For example, if you have data and you want show it in html, concatenation will cause you headache, while substitution will be simple and readable.

Python str.format versus concatenation, 1.3.4 String concatenation vs. format. Print. In GEOG 485, we used the + operator for string concatenation to produce strings from multiple components to then  It’s been awhile since I’ve written one of these “how to” articles, but I’m back at it. This time, I want to talk about string formatting using techniques like interpolation and concatenation. In other words, it’s time to finally learn how to format a string in Python. Table of Contents

Python 3.6 will introduce yet another option:

ACTOR_CACHE_KEY_PREFIX = 'actor_'

def get_actor_info(actor_id):
    cache_key = f'{ACTOR_CACHE_KEY_PREFIX}{actor_id}'

Performance should be comparable to '{}{}'.format(ACTOR_CACHE_KEY_PREFIX, actor_id), but is arguably more readable.

Python String Concatenation, Concatenation. In Python, there are a few ways to concatenate – or combine – strings. The new string that is created is referred to as a string  String Formatting in Python. In Python, we can take advantage of two separate methods of string interpolation. String interpolation is a term used to describe the process of evaluating a string value that is contained as one or more placeholders. To put it simply, it helps developers with string formatting and concatenation.

I guess that, if all the terms to concatenate are constants, the concatenation with the + operator might be optimized by python for performance. Ex.:

DB_PREFIX = 'prod_'
INDEX_PREFIX = 'index_'

CRM_IDX_PREFIX = DB_PREFIX + INDEX_PREFIX + 'crm_'

But most of the cases the format function and operators are used to concatenate with variable content. E.g:

crm_index_name = "{}_{}".format(CRM_IDX_PREFIX, index_id)

In practical terms, if you use the + operator to concatenate like this:

crm_index_name = CRM_IDX_PREFIX + '_' + str(index_id)

you are defining the format by custom code in a fixed way. If you use a format string with named references the code is more readable. E.g:

crm_index_name = "{db_prefix}_{idx_prefix}_{mod_prefix}_{id}".format(
   db_prefix=CRM_IDX_PREFIX,
   idx_prefix=INDEX_PREFIX,
   mod_prefix='crm',
   id=index_id,
)

That way you have the advantage to define the format as a constant. E.g:

IDX_FORMAT = "{db_prefix}_{idx_prefix}_{mod_prefix}_{id}"

crm_index_name = IDX_FORMAT.format(
   db_prefix=CRM_IDX_PREFIX,
   idx_prefix=INDEX_PREFIX,
   mod_prefix='crm',
   id=index_id,
)

And this result more clear in case that you need to change the format in the future. For example, in order to change the order of the separators you only need change the format string to:

IDX_FORMAT = "{db_prefix}_{mod_prefix}_{idx_prefix}-{id}"

As a plus, in order to debug you can assign all those variables to a dictionary and pass it as keyword parameters to the format function:

idx_name_parts = {
   'db_prefix': CRM_IDX_PREFIX,
   'idx_prefix': INDEX_PREFIX,
   'mod_prefix': 'crm',
   'id': index_id,
}
crm_index_name = IDX_FORMAT.format(**idx_name_parts)

Taking advantage of the globals() function we can also:

IDX_FORMAT = "{CRM_IDX_PREFIX}_{mod_prefix}_{INDEX_PREFIX}-{index_id}"

crm_index_name = IDX_FORMAT.format(mod_prefix = 'crm', **globals())

That is similar to the python3's formatted string literal:

crm_index_name = f"{CRM_IDX_PREFIX}_crm_{INDEX_PREFIX}-{index_id}"

I also see Internationalization as another use context where formatted expressions are more useful that + operator. Take the following code:

message = "The account " + str(account_number) + " doesn't exist"

if you use a translation feature like the gettext module with the + operator it would be:

message = _("The account ") + str(account_number) + _(" doesn't exist")

so it is better to translate the whole format string:

message = _("The account {account_number} doesn't exist").format(account_number)

so that the complete message has more sense in the spanish translation file:

#: main.py:523
msgid "The account {account_number} doesn't exist"
msgstr "La cuenta {account_number} no existe."

That is specially helpful in translation to natural languages whose grammatic impose change in the order of the sentence, like german language.

1.3.4 String concatenation vs. format, F-string is a syntax sugar to make manual string concatenation easier to read and write. However if we want to localize a composite string the string pattern must  Formatting Strings—Modulus . Although not actually modulus, the Python % operator works similarly in string formatting to interpolate variables into a formatting string. If you've programmed in C, you'll notice that % is much like C's printf(), sprintf(), and fprintf() functions.

String Concatenation and Formatting, In simple terms, string interpolation is a method which makes it easier for developers to execute string formatting and concatenation in Python. Python File Handling Python Read Files Python Write/Create Files Python Delete Files Python NumPy NumPy Intro NumPy Getting Started NumPy Creating Arrays NumPy Array Indexing NumPy Array Slicing NumPy Data Types NumPy Copy vs View NumPy Array Shape NumPy Array Reshape NumPy Array Iterating NumPy Array Join NumPy Array Split NumPy Array Search

Python 3's f-Strings: An Improved String Formatting Syntax (Guide , This time, I want to talk about string formatting using techniques like interpolation and concatenation. In other words, it's time to finally learn how to format a string  The format string syntax has become more powerful without complicating the simpler use cases. It pays off to read up on this string formatting mini-language in the Python documentation. In Python 3, this “new style” string formatting is to be preferred over %-style formatting.

Python String Concatenation And Formatting, These two principles come into contention when it comes to string operations. When concatenating strings, we can do the following: names = ['Jeff', 'Gary', 'Jill',  Before Python 3.6, you had two main ways of embedding Python expressions inside string literals for formatting: %-formatting and str.format(). You’re about to see how to use them and what their limitations are. Option #1: %-formatting. This is the OG of Python formatting and has been in the language since the very beginning.

Comments