{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Cheat Sheet\n", "\n", "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n", "\n", "## Read It\n", "\n", "- [Website](https://www.pythoncheatsheet.org)\n", "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n", "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n", "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n", "\n", "## Manipulating Strings\n", "\n", "### Escape Characters\n", "\n", "| Escape character | Prints as |\n", "| ---------------- | -------------------- |\n", "| `\\'` | Single quote |\n", "| `\\\"` | Double quote |\n", "| `\\t` | Tab |\n", "| `\\n` | Newline (line break) |\n", "| `\\\\` | Backslash |\n", "\n", "Example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Hello there!\\nHow are you?\\nI\\'m doing fine.\")\n", "Hello there!\n", "How are you?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Raw Strings\n", "\n", "A raw string completely ignores all escape characters and prints any backslash that appears in the string." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(r'That is Carol\\'s cat.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: mostly used for regular expression definition (see `re` package)\n", "\n", "### Multiline Strings with Triple Quotes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('''Dear Alice,\n", "\n", "Eve's cat has been arrested for catnapping, cat burglary, and extortion.\n", "\n", "Sincerely,\n", "Bob''')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To keep a nicer flow in your code, you can use the `dedent` function from the `textwrap` standard package." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from textwrap import dedent\n", "\n", "def my_function():\n", " print('''\n", " Dear Alice,\n", "\n", " Eve's cat has been arrested for catnapping, cat burglary, and extortion.\n", "\n", " Sincerely,\n", " Bob\n", " ''').strip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This generates the same string than before.\n", "\n", "### Indexing and Slicing Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "H e l l o w o r l d !\n", "0 1 2 3 4 5 6 7 8 9 10 11" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam = 'Hello world!'\n", "spam[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam[4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slicing:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "spam[0:5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam[:5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam[6:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam[6:-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam[:-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam[::-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam = 'Hello world!'\n", "fizz = spam[0:5]\n", "fizz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The in and not in Operators with Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello' in 'Hello World'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello' in 'Hello'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'HELLO' in 'Hello World'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'' in 'spam'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'cats' not in 'cats and dogs'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The in and not in Operators with list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3, 4]\n", "5 in a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 in a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The upper, lower, isupper, and islower String Methods\n", "\n", "`upper()` and `lower()`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam = 'Hello world!'\n", "spam = spam.upper()\n", "spam" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam = spam.lower()\n", "spam" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isupper() and islower():" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam = 'Hello world!'\n", "spam.islower()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam.isupper()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'HELLO'.isupper()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'abc12345'.islower()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'12345'.islower()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'12345'.isupper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The isX String Methods\n", "\n", "- **isalpha()** returns True if the string consists only of letters and is not blank.\n", "- **isalnum()** returns True if the string consists only of lettersand numbers and is not blank.\n", "- **isdecimal()** returns True if the string consists only ofnumeric characters and is not blank.\n", "- **isspace()** returns True if the string consists only of spaces,tabs, and new-lines and is not blank.\n", "- **istitle()** returns True if the string consists only of wordsthat begin with an uppercase letter followed by onlylowercase letters.\n", "\n", "### The startswith and endswith String Methods" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello world!'.startswith('Hello')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello world!'.endswith('world!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'abc123'.startswith('abcdef')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'abc123'.endswith('12')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello world!'.startswith('Hello world!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello world!'.endswith('Hello world!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The join and split String Methods\n", "\n", "join():" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "', '.join(['cats', 'rats', 'bats'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "' '.join(['My', 'name', 'is', 'Simon'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'ABC'.join(['My', 'name', 'is', 'Simon'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "split():" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'My name is Simon'.split()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'MyABCnameABCisABCSimon'.split('ABC')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'My name is Simon'.split('m')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Justifying Text with rjust, ljust, and center\n", "\n", "rjust() and ljust():" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello'.rjust(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello'.rjust(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello World'.rjust(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello'.ljust(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An optional second argument to rjust() and ljust() will specify a fill character other than a space character. Enter the following into the interactive shell:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello'.rjust(20, '*')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello'.ljust(20, '-')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "center():" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello'.center(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello'.center(20, '=')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Removing Whitespace with strip, rstrip, and lstrip" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam = ' Hello World '\n", "spam.strip()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam.lstrip()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam.rstrip()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam = 'SpamSpamBaconSpamEggsSpamSpam'\n", "spam.strip('ampS')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Copying and Pasting Strings with the pyperclip Module\n", "\n", "First, install `pypeerclip` with pip:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "attributes": { "classes": [ "shell" ], "id": "" } }, "outputs": [], "source": [ "pip install pyperclip" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pyperclip\n", "\n", "pyperclip.copy('Hello world!')\n", "pyperclip.paste()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }