{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 004_strings\n", "\n", "[Source](https://github.com/iArunava/Python-TheNoTheoryGuide/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import the string module to get all the in-built helper methods for string\n", "import string" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Case change of string variables\n", "name = \"jennifEr loves Python\"\n", "\n", "# Title case\n", "name_t = name.title() \n", "print (name_t)\n", "\n", "# Upper case\n", "name_t = name.upper() \n", "print (name_t)\n", "\n", "# Lower case\n", "name_t = name.lower() \n", "print (name_t)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# String Concatenation\n", "fname = \"jennifer\"\n", "lname = \"python\"\n", "flname = fname + \" \" + lname\n", "\n", "print (fname + \" \" + lname)\n", "print (\"Jennifer\" + \" \" + \"Python\")\n", "# OR equivalently\n", "print (flname.title())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding WhiteSpaces\n", "print (\"Jen\\nloves\\npython\")\n", "print (\"Jen\\tloves\\tpython\")\n", "print (\"Jen\\tloves\\npython\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Stripping Whitespace\n", "name1 = \" Jennifer\"\n", "name2 = \"Jennifer \"\n", "name3 = \" Jennifer \"\n", "\n", "print (name1)\n", "print (name2)\n", "print (name3)\n", "print (\"----------\")\n", "print (name1.lstrip()) # lstrip() takes all extra whitespaces from left\n", "print (name2.rstrip()) # rstrip() takes all extra whitespaces from right\n", "print (name3.strip()) # strip() takes all extra whitespaces from both left and right" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# str() -> casts other data types to string\n", "jensage = 21\n", "# print (\"Jennifer's age is \" + jensage) ## This is an error as one cannot concatenate string and integer\n", "print (\"Jennifer's age is \" + str(jensage)) # This works!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Lists, Dictionaries, Tuples and other objects can be casted to String\n", "l_langs = [\"Python\", \"R\", \"Julia\"] # List\n", "t_langs = (\"Python\", \"R\", \"Julia\") # Tuple\n", "d_langs = {1: \"Python\", 2: \"R\", 3: \"Julia\"} # Dictionary\n", "\n", "print (\"This is a list: \" + str(l_langs))\n", "print (\"This is a tuple: \" + str(t_langs))\n", "print (\"This is a dictionary: \" + str(d_langs))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Some helpful constants built-in the string module\n", "print (\"All Letters: \" + string.ascii_letters)\n", "print (\"Lowecase: \" + string.ascii_lowercase)\n", "print (\"Uppercase: \" + string.ascii_uppercase)\n", "print (\"Punctuations: \" + string.punctuation)\n", "print (\"Numbers: \" + string.digits)\n", "print (\"Hex Digits: \" + string.hexdigits)\n", "print (\"Oct Digits: \" + string.octdigits)\n", "print (\"Whitespace: \" + string.whitespace)\n", "print (\"Printable: \" + string.printable)" ] } ], "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }