|
1 |
| -TODO: add information on strings concept |
| 1 | +Pythons string type `str` can be very powerful. At its core, a `str` is an immutable [text sequence](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str) of [Unicode code points](https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme). There is no separate "character" or "char" type in Python. |
| 2 | + |
| 3 | +Like any [sequence type](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range), code points or "characters" within a string can be referenced by 0-based index number, and can be copied in whole or in part via _slice notation_. Since there is no separate “character” type, indexing a string produces a new `str` of length 1 (_for example "exercism"[0] == "exercism"[0:1] == "e"_). Strings support all [common sequence operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations), and individual code points or "characters" can be iterated through in a loop via `for item in`. |
| 4 | + |
| 5 | +For a deep dive on what information a `str` encodes (or, _"how does the computer know how to translate zeroes and ones into letters?"_), [this blog post is enduringly helpful][joel-on-text]. |
| 6 | + |
| 7 | +Strings can be transformed by [various methods](https://docs.python.org/3/library/stdtypes.html#string-methods), split into letters/symbols, and joined together via [`.join()`](https://docs.python.org/3/library/stdtypes.html#str.join) or `+` to create larger strings . Due to their immutability, any transformations applied to a `str` return a new `str`. |
| 8 | + |
| 9 | +### Construction |
| 10 | + |
| 11 | +The simplest way to create a `str` literal is by delimiting it with `"` or `'`. Strings can also be written across multiple lines by using triple quotes (`"""` or `'''`) . |
| 12 | + |
| 13 | +````python |
| 14 | +single_quoted = 'Single quotes allow "double quoting" without "escape" characters.' |
| 15 | + |
| 16 | +double_quoted = "Double quotes allow embedded 'single quoting' without 'escape' characters". |
| 17 | + |
| 18 | +triple_quoted = '''Three single quotes or double quotes in a row allow for multi-line string literals. You will most often encounter these as "doc strings" or "doc tests" written just below the first line of a function or class definition. They are often used with auto documentation tools.''' |
| 19 | +String literals that are part of a single expression and are separated only by white space are _implicitly concatenated_ into a single string literal: |
| 20 | + |
| 21 | +```python |
| 22 | +("I do not " |
| 23 | +"like " |
| 24 | +"green eggs and ham.") == "I do not like green eggs and ham."``` |
| 25 | + |
| 26 | + |
| 27 | +Additionally, [interpolated](https://en.wikipedia.org/wiki/String_interpolation) strings (`f-strings`) can be formed: |
| 28 | + |
| 29 | +```python |
| 30 | +my_name = "Praveen" |
| 31 | + |
| 32 | +intro_string = f"Hi! My name is {my_name}, and I'm happy to be here!" |
| 33 | + |
| 34 | +>>>print(intro_string) |
| 35 | +Hi! My name is Praveen, and I'm happy to be here!``` |
| 36 | + |
| 37 | +Finally, the [`str()` constructor](https://docs.python.org/3/library/stdtypes.html#str) can be used to create/coerce strings from other objects/types. However, the `str` constructor _**will not iterate**_ through an object , so if something like a `list` of elements needs to be connected, `.join()` is a better option: |
| 38 | + |
| 39 | +```python |
| 40 | +>>> my_number = 675 |
| 41 | +>>> str(my_number) |
| 42 | +'675' |
| 43 | + |
| 44 | +>>> my_list = ["hen", "egg", "rooster"] |
| 45 | +>>> str(my_list) |
| 46 | +"['hen', 'egg', 'rooster']" |
| 47 | + |
| 48 | +>>> ' '.join(my_list) |
| 49 | +'hen egg rooster'``` |
| 50 | + |
| 51 | + |
| 52 | +### Formatting |
| 53 | + |
| 54 | +Python provides a rich set of tools for [formatting](https://docs.python.org/3/library/string.html#custom-string-formatting) and [templating](https://docs.python.org/3/library/string.html#template-strings) strings, as well as more sophisticated text processing through the [re (_regular expressions_)](https://docs.python.org/3/library/re.html), [difflib (_sequence comparison_)](https://docs.python.org/3/library/difflib.html), and [textwrap](https://docs.python.org/3/library/textwrap.html) modules. For a great introduction to string formatting in Python, see [this post at Real Python](https://realpython.com/python-string-formatting/). |
| 55 | + |
| 56 | +For more details on string methods, see [Strings and Character Data in Python](https://realpython.com/python-strings/) at the same site. |
| 57 | + |
| 58 | +### Related types and encodings |
| 59 | + |
| 60 | +In addition to `str` (a *text* sequence), Python has corresponding [binary sequence types](https://docs.python.org/3/library/stdtypes.html#binaryseq) `bytes` (a *binary* sequence), `bytearray` and `memoryview` for the efficient storage and handling of binary data. Additionally, [Streams](https://docs.python.org/3/library/asyncio-stream.html#streams) allow sending and receiving binary data over a network connection without using callbacks. |
| 61 | +```` |
0 commit comments