diff options
Diffstat (limited to 'lessons/string formatting.ipynb')
| -rw-r--r-- | lessons/string formatting.ipynb | 358 |
1 files changed, 0 insertions, 358 deletions
diff --git a/lessons/string formatting.ipynb b/lessons/string formatting.ipynb deleted file mode 100644 index eec5792..0000000 --- a/lessons/string formatting.ipynb +++ /dev/null @@ -1,358 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "player = 'thomas'" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "points = 33" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "can only concatenate str (not \"int\") to str", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m<ipython-input-3-1da050906281>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplayer\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mpoints\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" - ] - } - ], - "source": [ - "print(player + points)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "thomas33\n" - ] - } - ], - "source": [ - "# concat\n", - "print(player + str(points))" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "thomas 33\n" - ] - } - ], - "source": [ - "# string formatting with f-strings. F-strings are a new feature of python3.\n", - "print(f'{player} {points}')" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "sentence = player + \" \" + str(points) + \".\"" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "thomas 33.\n" - ] - } - ], - "source": [ - "print(sentence)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 33 thomas 33.\n" - ] - } - ], - "source": [ - "sentence2 = f' {points} {player} {points}.'\n", - "print(sentence2)" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "33 thomas 33.\n" - ] - } - ], - "source": [ - "# In older versions of Python, f-strings did not exist. Instead we did\n", - "print(\"%s %s %s.\" % (points, player, points))" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I scored 33.3 points today.\n", - "I scored 33 points today.\n" - ] - } - ], - "source": [ - "# %s converts everything into string\n", - "# %d converts into numbers (integer first, without rounding)\n", - "print('I scored %s points today.' % 33.3)\n", - "print('I scored %d points today.' % 33.8)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Padding and Precision of floating point numbers\n", - "\n", - "floating points use the format %5.2f. Here, 5 would be minimum number of characters the string should contain. .2f stands for how many numbers to show AFTER the decimal point." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Floating point numbers 13.49\n" - ] - } - ], - "source": [ - "print('Floating point numbers %5.2f' % (13.490))" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Floating point numbers: 13\n" - ] - } - ], - "source": [ - "print('Floating point numbers: %1.0f' %(13.144))" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Floating point numbers: 13.14\n" - ] - } - ], - "source": [ - "print('Floating point numbers: %25.2f' %(13.144))\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Multi Formatting" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "First hi, Second: 3.14\n" - ] - } - ], - "source": [ - "print('First %s, Second: %5.2f' % ('hi', 3.1415))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Formatting with .format() method" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "this is a string with an insert\n" - ] - } - ], - "source": [ - "print(\"this is a string with an {}\".format('insert'))" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The quick brown fox\n" - ] - } - ], - "source": [ - "# (0,1,2) == index(fox, brown, quick)\n", - "print('The {2} {1} {0}'.format('fox', 'brown', 'quick'))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Float formatting with f-strings" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My 10 character, four decimal number is: 23.4568\n" - ] - } - ], - "source": [ - "num = 23.45678\n", - "print(f'My 10 character, four decimal number is:{num:{10}.{6}}')" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My 10 character, four decimal number is: 23.4568\n" - ] - } - ], - "source": [ - "print(f'My 10 character, four decimal number is:{num:10.4f}')" - ] - }, - { - "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.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} |
