The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ: Why is join() a string method instead of a list or tuple method? How does string join work in Python? (tutorial with examples)
By Guest on 9th November 2022 12:06:11 AM | Syntax: PYTHON | Views: 192



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. How does string join work in Python?
  2.  
  3. Why is join() a string method instead of a list or tuple method?
  4. ================================================================
  5.  
  6. Strings became much more like other standard types starting in Python
  7. 1.6, when methods were added which give the same functionality that
  8. has always been available using the functions of the string module.
  9. Most of these new methods have been widely accepted, but the one which
  10. appears to make some programmers feel uncomfortable is:
  11.  
  12.    ", ".join(['1', '2', '4', '8', '16'])
  13.  
  14. which gives the result:
  15.  
  16.    "1, 2, 4, 8, 16"
  17.  
  18. There are two common arguments against this usage.
  19.  
  20. The first runs along the lines of: "It looks really ugly using a
  21. method of a string literal (string constant)", to which the answer is
  22. that it might, but a string literal is just a fixed value. If the
  23. methods are to be allowed on names bound to strings there is no
  24. logical reason to make them unavailable on literals.
  25.  
  26. The second objection is typically cast as: "I am really telling a
  27. sequence to join its members together with a string constant".  Sadly,
  28. you aren't.  For some reason there seems to be much less difficulty
  29. with having "split()" as a string method, since in that case it is
  30. easy to see that
  31.  
  32.   "1, 2, 4, 8, 16".split(", ")
  33.  
  34. is an instruction to a string literal to return the substrings
  35. delimited by the given separator (or, by default, arbitrary runs of
  36. white space).
  37.  
  38. "join()" is a string method because in using it you are telling the
  39. separator string to iterate over a sequence of strings and insert
  40. itself between adjacent elements.  This method can be used with any
  41. argument which obeys the rules for sequence objects, including any new
  42. classes you might define yourself. Similar methods exist for bytes and
  43. bytearray objects.
  44.  
  45. Python - The split() and join() methods - String Tutorial with Example
  46.  
  47. Combining List Elements with join Method




  48.  
  49. [inline]
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes