Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fee9fcd

Browse files
author
Kenneth Reitz
committed
Merge pull request realpython#261 from ozgur/string_concatenation_note
Note added on string concatenation
2 parents 08b8eec + 328df72 commit fee9fcd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/writing/structure.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,23 @@ should be your preferred method.
463463
foo += 'ooo' # This is bad, instead you should do:
464464
foo = ''.join([foo, 'ooo'])
465465
466+
.. note::
467+
You can also use the **%** formatting operator to concatenate the
468+
pre-determined number of strings besides **join()** and **+**. However,
469+
according to `PEP 3101 <http://www.python.org/dev/peps/pep-3101/>`_,
470+
**%** operator became deprecated in Python 3.1 and will be replaced by the
471+
**format()** method in the later versions.
472+
473+
.. code-block:: python
474+
475+
foo = 'foo'
476+
bar = 'bar'
477+
478+
foobar = '%s%s' % (foo, bar) # It is OK
479+
foobar = '{0}{1}'.format(foo, bar) # It is better
480+
foobar = '{foo}{bar}'.format(foo=foo, bar=bar) # It is best
481+
482+
466483
Vendorizing Dependencies
467484
------------------------
468485

0 commit comments

Comments
 (0)