445 lines
10 KiB
Plaintext
445 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Python String format()\n",
|
|
"[Official docs](https://docs.python.org/3/library/string.html#format-string-syntax) \n",
|
|
"[more documentation](https://pyformat.info)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Replace with String - positional"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"My name is Alex.\n",
|
|
"My name is Alex Marshall.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"first_name = 'Alex'\n",
|
|
"last_name = 'Marshall'\n",
|
|
"print('My name is {}.'.format(first_name))\n",
|
|
"print('My name is {} {}.'.format(first_name, last_name))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Replace with String using Index\n",
|
|
"Using indexes can be useful when order varies."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"My name is Alex Marshall.\n",
|
|
"My name is Marshall, Alex. First name Alex\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print('My name is {0} {1}.'.format(first_name, last_name))\n",
|
|
"print('My name is {1}, {0}. First name {0}'.format(first_name, last_name))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Alignment: Align Left, Right, Middle\n",
|
|
"{:<} align Left (default is align left, so this is optional) \n",
|
|
"{:>n} align Right with n padding spaces \n",
|
|
"{:^n} align Middle with n padding spaces"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Number of cases: 5\n",
|
|
"Number of cases: 16\n",
|
|
"Number of cases: 294\n",
|
|
"Number of cases: 5\n",
|
|
"Number of cases: 16\n",
|
|
"Number of cases: 294\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# align left - these both do the same thing\n",
|
|
"cases = [5, 16, 294]\n",
|
|
"for case in cases:\n",
|
|
" print('Number of cases: {}'.format(case))\n",
|
|
"for case in cases:\n",
|
|
" print('Number of cases: {:<}'.format(case))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Number of cases: 5\n",
|
|
"Number of cases: 16\n",
|
|
"Number of cases: 294\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# align right with 5 total spaces\n",
|
|
"for case in cases:\n",
|
|
" print('Number of cases:{:>5}'.format(case))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Number of cases: 5 \n",
|
|
"Number of cases: 16 \n",
|
|
"Number of cases: 294 \n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# align center with 5 total spaces\n",
|
|
"for case in cases:\n",
|
|
" print('Number of cases:{:^5}'.format(case))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Integers & Floats\n",
|
|
"{:d} Integer variable \n",
|
|
"{:5d} Integer with padding of 5 \n",
|
|
"{:f} Floating point variable "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Length is 26.\n",
|
|
"Length is 26.\n",
|
|
"In dog years, I'm 8 .\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"length = 26\n",
|
|
"print('Length is {:d}.'.format(length))\n",
|
|
"\n",
|
|
"# align right, padding=6, integer\n",
|
|
"print('Length is {:>6d}.'.format(length))\n",
|
|
"\n",
|
|
"# named variable, align center, padding=4, integer\n",
|
|
"print(\"In dog years, I'm {age:^5d}.\".format(age=8))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Distance to moon is 238,900 miles.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# integer with commas\n",
|
|
"print('Distance to moon is {:,d} miles.'.format(238900))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Radius is 4.780000 inches.\n",
|
|
"Radius is 4.8 inches.\n",
|
|
"Radius is 0004.8 inches.\n",
|
|
"Radius is 4.78000 inches.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"radius = 4.78\n",
|
|
"print('Radius is {:f} inches.'.format(radius))\n",
|
|
"\n",
|
|
"# round to 1 decimal place, float\n",
|
|
"print('Radius is {:.1f} inches.'.format(radius))\n",
|
|
"\n",
|
|
"# padding=6 (pads with leading 0's), round to 1 decimal\n",
|
|
"print('Radius is {:06.1f} inches.'.format(radius))\n",
|
|
"\n",
|
|
"# padding=5 decimal places\n",
|
|
"print('Radius is {:.5f} inches.'.format(radius))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"A is +15. B is -9. C is 33.\n",
|
|
"A is +15. B is -9. B is -9.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# positive & negative signs\n",
|
|
"a, b, c = 15, -9, 33\n",
|
|
"print('A is {:+d}. B is {:+d}. C is {:-d}.'.format(a, b, c))\n",
|
|
"\n",
|
|
"# {+3d} shows pos or neg sign, padding=3. \n",
|
|
"# {: d} prints neg or a leading space if positive.\n",
|
|
"print('A is {:+3d}. B is {:+4d}. B is {: d}.'.format(a, b, b))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Named Placeholders\n",
|
|
"You can pass in named variables as keyword args, or as an unpacked dict. \n",
|
|
"And it's easy to pass in a list."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Mekael is a Carpenter.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(\"{name} is a {job}.\".format(name='Mekael', job='Carpenter'))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "KeyError",
|
|
"evalue": "'name'",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[0;32m<ipython-input-11-be610b0b3133>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mjob\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Carpenter'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# THIS DOES NOT WORK!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"{name} is a {job}.\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mjob\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
"\u001b[0;31mKeyError\u001b[0m: 'name'"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"name = 'Mekael'\n",
|
|
"job = 'Carpenter'\n",
|
|
"# THIS DOES NOT WORK!\n",
|
|
"print(\"{name} is a {job}.\".format(name, job))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Mekael is a Carpenter.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# This works great\n",
|
|
"print(\"{n} is a {j}.\".format(n=name, j=job))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Mekael is a Carpenter.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Or use a dictionary, and ** unpacks the dictionary.\n",
|
|
"jobs = {'name':'Mekael', 'job':'Carpenter'}\n",
|
|
"print(\"{name} is a {job}.\".format(**jobs))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Score 2 is 96\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# passing in a list is clean and easy\n",
|
|
"scores = [78, 96, 83, 86]\n",
|
|
"print('Score 2 is {s[1]}'.format(s = scores))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Scientific Notation\n",
|
|
"Use {:e}, or upper case E."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"My big number is 8.745770e+02\n",
|
|
"A bigger number is 6.022141E+23\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print('My big number is {:e}'.format(874.577))\n",
|
|
"print('A bigger number is {:E}'.format(602214090000000000000000))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Binary & Hexadecimal\n",
|
|
"{:b} converts decimal to binary\n",
|
|
"{:x} converts decimal to hex. Or use upper case X for capitals."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The binary equivalent of 79 is 1001111\n",
|
|
"The Hexadecimal equivalent of 183 is B7\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print('The binary equivalent of 79 is {:b}'.format(79))\n",
|
|
"print('The Hexadecimal equivalent of 183 is {:X}'.format(183))"
|
|
]
|
|
},
|
|
{
|
|
"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
|
|
}
|