Files
Python/Strings/Using Python Strings.ipynb
2020-05-04 17:51:56 -07:00

519 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using Strings in Python 3\n",
"[Python String docs](https://docs.python.org/3/library/string.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating Strings\n",
"Enclose a string in single or double quotes, or in triple single quotes. \n",
"And you can embed single quotes within double quotes, or double quotes within single quotes. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tony Stark is Ironman.\n",
"Her book is called \"The Magician\".\n",
"Captain Rogers kicks butt.\n"
]
}
],
"source": [
"s = 'Tony Stark is'\n",
"t = \"Ironman.\"\n",
"print(s, t)\n",
"u = 'Her book is called \"The Magician\".'\n",
"print(u)\n",
"v = '''Captain Rogers kicks butt.'''\n",
"print(v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Type, Len, Split, Join\n",
"Get the number of characters in a string using len. \n",
"To get the number of words you have to split the string into a list. Split uses a space as its default, or you can split on any substring you like. \n",
"To reverse a split, use join(str)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'str'>\n",
"13\n"
]
}
],
"source": [
"print(type(s))\n",
"print(len(s))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Tony', 'Stark', 'is']\n",
"3\n",
"['Her book is c', 'lled \"The M', 'gici', 'n\".']\n",
"['you', 'are', 'so', 'pretty']\n",
"Just do it.\n"
]
}
],
"source": [
"print(s.split())\n",
"print(len(s.split()))\n",
"print(u.split('a'))\n",
"print('you,are,so,pretty'.split(','))\n",
"print(' '.join(['Just', 'do', 'it.']))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Check if a substring is contained in a string\n",
"Use *in* or *not in*. \n",
"Startswith and Endswith are also useful boolean checks."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"False\n",
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"print('dog' in s)\n",
"print('k' in t)\n",
"print('k' not in t)\n",
"print(s.startswith('Tony'))\n",
"print(s.endswith('is'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Replace all substrings\n",
"Second example iterates through a dictionary and replaces all instances of text numbers with numerals."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Captain America kicks butt.\n"
]
}
],
"source": [
"v = v.replace('Rogers', 'America')\n",
"print(v)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Anton has 3 cars. Javier has 4.\n"
]
}
],
"source": [
"z = 'Anton has three cars. Javier has four.'\n",
"numbers = {'one':'1', 'two':'2', 'three':'3', 'four':'4', 'five':'5'}\n",
"for k,v in numbers.items():\n",
" z = z.replace(k,v)\n",
"print(z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Change case"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tony stark is\n",
"IRONMAN.\n",
"Her Book Is Called \"The Magician\".\n",
"Hulk rules!\n"
]
}
],
"source": [
"print(s.lower())\n",
"print(t.upper())\n",
"print(u.title())\n",
"print('hulk rules!'.capitalize())"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n",
"True\n"
]
}
],
"source": [
"print('david'.islower())\n",
"print('hulk'.isupper())\n",
"print('Hulk'.istitle())\n",
"print('covid19'.isalnum())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n",
"False\n"
]
}
],
"source": [
"print('Thor'.isalpha())\n",
"print('3.14'.isnumeric())\n",
"print('314'.isdigit())\n",
"print('3.14'.isdecimal())"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0123456789\n",
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\n",
"abcdefghijklmnopqrstuvwxyz\n",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
]
}
],
"source": [
"import string\n",
"print(string.digits)\n",
"print(string.punctuation)\n",
"print(string.ascii_lowercase)\n",
"print(string.ascii_uppercase)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Strip leading or trailing characters\n",
"This is often used to strip blank spaces or newlines, but can be used for much more."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Natasha is a spy.\n",
"Natasha is a spy \n",
"\n",
"\n",
" Natasha is a spy\n",
"Natasha is a spy. She has red hair.\n",
"She has red \n"
]
}
],
"source": [
"w = '\\n Natasha is a spy \\n'\n",
"x = '\\nShe has red hair\\n'\n",
"\n",
"print(w.strip() + '.')\n",
"print(w.lstrip())\n",
"print(w.rstrip())\n",
"print(w.strip() + '. ' + x.strip() + '.')\n",
"print(x.strip().rstrip('arih'))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What do you want\n"
]
}
],
"source": [
"y = 'What do you want?!!&?'\n",
"print(y.rstrip(string.punctuation))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Find, and Count substrings\n",
"Search from the left with find, or from the right with rfind. \n",
"The return value is the start index of the first match of the substring."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"5\n",
"What do you want?!!&?\n"
]
}
],
"source": [
"print(y.find('a'))\n",
"print(y.rfind('do'))\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Natasha is a spy\n",
"4\n"
]
}
],
"source": [
"print(w.strip())\n",
"print(w.count('a'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Strings are immutable\n",
"Any change to a string results in a new string being written to a new block of memory. "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4565794160\n",
"4565793776\n"
]
}
],
"source": [
"m = 'Black widow'\n",
"print(id(m))\n",
"m = m + 's'\n",
"print(id(m))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tony Stark is Ironman.\n",
"Tony Stark is Ironman.\n"
]
}
],
"source": [
"print(s, t)\n",
"z = s + ' ' + t\n",
"print(z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Slicing Substrings\n",
"string[from:to+1:step]\n",
"Only 1 parameter: it is used as an index. \n",
"From defaults to beginning. \n",
"To defaults to end. \n",
"Step defaults to 1."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"567\n"
]
}
],
"source": [
"z = '0123456789'\n",
"print(z[1])\n",
"print(z[5:8])"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"012\n",
"789\n",
"89\n",
"24\n"
]
}
],
"source": [
"print(z[:3])\n",
"print(z[7:])\n",
"print(z[-2:])\n",
"print(z[2:5:2])"
]
},
{
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}