mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
Merge pull request #261 from ozgur/string_concatenation_note
Note added on string concatenation
This commit is contained in:
@@ -463,6 +463,23 @@ should be your preferred method.
|
|||||||
foo += 'ooo' # This is bad, instead you should do:
|
foo += 'ooo' # This is bad, instead you should do:
|
||||||
foo = ''.join([foo, 'ooo'])
|
foo = ''.join([foo, 'ooo'])
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
You can also use the **%** formatting operator to concatenate the
|
||||||
|
pre-determined number of strings besides **join()** and **+**. However,
|
||||||
|
according to `PEP 3101 <http://www.python.org/dev/peps/pep-3101/>`_,
|
||||||
|
**%** operator became deprecated in Python 3.1 and will be replaced by the
|
||||||
|
**format()** method in the later versions.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
foo = 'foo'
|
||||||
|
bar = 'bar'
|
||||||
|
|
||||||
|
foobar = '%s%s' % (foo, bar) # It is OK
|
||||||
|
foobar = '{0}{1}'.format(foo, bar) # It is better
|
||||||
|
foobar = '{foo}{bar}'.format(foo=foo, bar=bar) # It is best
|
||||||
|
|
||||||
|
|
||||||
Vendorizing Dependencies
|
Vendorizing Dependencies
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user