Summary

  • The str Unicode strings and bytes strings:

    • We looked at the various forms of quotes (single or double quotation marks) for quoting strings, useful for incorporating quote marks themselves into strings. Python is flexible over which quoting style you use, but you must be consistent when delimiting a particular string.

    • We demonstrated that so-called triple quotes, consisting of three consecutive quotation mark characters can be used to delimit a multi-line string. Traditionally, each quote character is itself a double quotation mark, although single quotation marks can also be used.

    • We saw how adjacent string literals are implicitly concatenated.

    • Python has support for universal newlines, so no matter what platform
      you're using it's sufficient to use a single character, safe in the
      knowledge that is will be appropriately translated from and to the native
      newline during I/O.

    • Escape sequences provide an alternative means of incorporating newlines and other control characters into literal strings.

    • The backslashes used for escaping can be a hindrance for Windows filesystem paths or regular expressions, so raw strings with an r prefix can be used to suppress the escaping mechanism.

    • Other types, such as integers, can be converted to strings using the str() constructor.

    • Individual characters, returned as one character strings, can be retrieved using square brackets with integer zero-based indices.

    • Strings support a rich variety of operations, such as splitting, through their methods.

    • In Python 3, literal strings can contain any Unicode character directly in the source, which is interpreted as UTF-8 by default.

    • The bytes type has many of the capabilities of strings, but it is a
      sequence as bytes rather than a sequence of Unicode code points.

    • The bytes literals are prefixed with a lowercase b.

    • To convert between string and bytes instances we use the encode() method of str or the decode() method of bytes, in both cases passing the name of the codec, which we must know in advance.

  • The list literal

    • Lists are mutable, heterogeneous sequences of objects.

    • The list literals are delimited by square brackets and the items are separated by commas.

    • Individual elements can be retrieved by indexing into a list with square brackets containing a zero-based integer index.

    • In contrast to strings individual list elements can be replaced by assigning to the indexed item.

    • Lists can be grown by append()-ing to them, and can be constructed from other sequences using the list() constructor.

  • dict

    • Dictionaries associate keys with values.

    • Literal dictionaries are delimited by curly braces. The key-value pairs are separated from each other by commas, and each key is associated with its corresponding value with a colon.

  • The for loops

    • The for-loops take items one-by-one from an iterable object such as a list, and bind the same name to the current item.

    • They correspond to what are called for-each loops in other languages.

We don't cover regular expressions – also known as regexes – in this book. See the documentation for the Python Standard Library re module for more information. https://docs.python.org/3/library/re.html.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset