{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Special characters\n", "\n", "When we use strings, you will notice that we often want to use some \"_special characters_\". These special characters consist of the backslash character (`\\`) followed by another character.\n", "\n", "**Tab character** `\\t`: For example, if we want to create an output of multiple columns, with each columns being separated with a tab from each other, we can use the tab character, which is represented as `\\t`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Example: List the first name, last name, and email of a person, in columns\n", "# Separate the columns using a tab character\n", "str_4a =\"First Name\\tLast Name\\tEmail\"\n", "str_4b =\"Panagiotis\\tIpeirotis\\tpanos@nyu.edu\"\n", "str_4c =\"Kristaps\\tPorzingis\\tkporzee@nba.com\"\n", "print(str_4a)\n", "print(str_4b)\n", "print(str_4c)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**New line character** `\\n`: This is a special character that we use to represent a new line." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Notice that we end the strings below with the \\n character, \n", "# which is the \"new line\" special character\n", "str_5 = \"Hello World!\\nHello World Twice!\"\n", "print(str_5)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Backslash character** `\\\\`: In general, backslash (`\\`) is used to introduce special characters. If we want to type the backslash character itself, we do it by typing backslash twice in a row: `\\\\`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"I want to print backslash: \\\\\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Quotes**: " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_4 = 'This is a string within single quotes that can contain \"double quotes\" as part of the string'\n", "print(str_4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_5 = 'If we want to have \\'single quotes\\' in single quoted string we should escape them'\n", "print(str_5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_6 = \"Similarly, if we want to have \\\"double quotes\\\" in double quoted string we should escape them\\n\"\n", "print(str_6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Splitting Strings: `split` and `join`\n", "\n", "Since we talked about special characters, let’s talk now about splitting strings, using the `split()` function.\n", "\n", "\n", "+ `longstring.split(separator)`: split the first string (longstring) at every occurrence of the second string (separator) Outputs a list (see below).\n", "+ `connector.join(list)`: join is the \"reverse\" of split, and joins all the elements of the list, using the `connector` string in front." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"practical data science\".split(\" \"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"billgates@microsoft.com\".split(\"@\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or let's take the example from above:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_a =\"First Name\\tLast Name\\tEmail\"\n", "str_b =\"Panagiotis\\tIpeirotis\\tpanos@nyu.edu\"\n", "str_c =\"Kristaps\\tPorzingis\\tkporzee@nba.com\"\n", "print(str_a.split('\\t'))\n", "print(str_b.split('\\t'))\n", "print(str_c.split('\\t'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that when we split a string and the delimeter character\n", "does not appear, then we get back the string itself, but converted\n", "into a list with a single element. (We will talk about lists in a \n", " couple of weeks)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"hello\".split(\" \"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Join is the \"reverse\" of split, and joins all\n", " the elements of the list, using the \"string\" in front.\n", " The command below joins the elements of the `line` variable using the \n", " characters `###` as the connecting element." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "line = ['Panagiotis', 'Ipeirotis', 'panos@nyu.edu']\n", "\"###\".join(line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise\n", "\n", "Consider the string `billgates@microsoft.com`. Write code that finds the username of the email address and the domain of the email address, using the `split()` command." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "email = \"billgates@microsoft.com\"\n", "result = email.split(\"@\")\n", "print(result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# We can also access the individual elements of a list using indexing\n", "print(\"Username:\", result[0])\n", "print(\"Domain:\", result[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "email = \"billgates@microsoft.com\"\n", "result = email.split(\"@\")\n", "print(result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# And if we want to access the individual elements of a list we can use the indexing notation, as in the case of strings:\n", "print(\"Username:\", result[0])\n", "print(\"Domain:\", result[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }