{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Note: maybe you should determine length of \"longest\" fname and lname,\n", "say max_fname and then format the the fname column in max_fname+1 spaces\n", "Similarly for lname\n", " \n", "col1: max_fname + 1\n", "col2: max_lname + 1\n", "col3: 18 + 1 = 19\n", "col4: 9 + 1 = 10\n", "col5: 19\n", "col6: 10\n", "col7: 8\n", "\n", "\n", "january\n", "february\n", "march\n", "april\n", "may\n", "june\n", "july\n", "august\n", "september 9\n", "october\n", "november\n", "december\n", "\n", "\n", "print(\"{:(max_fname+1)s}{:(max_fname+1)s}{:19s}{:10s}{:19s}{:10s}{:8d}\".format(a,b,c,d,e,f,g))\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a = \"Srinivasa\"\n", "b = \"Ramanujan\"\n", "c = \"22 December, 1887\"\n", "d = \"Thursday\"\n", "e = \"26 April, 1920\"\n", "f = \"Monday\"\n", "g = 11813" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "max_fname = 9\n", "max_lname = 11" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Srinivasa Ramanujan 22 December, 1887 Thursday 26 April, 1920 Monday 11813\n" ] } ], "source": [ "print(\"{:10s}{:12s}{:19s}{:10s}{:19s}{:10s}{:8d}\".format(a,b,c,d,e,f,g))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Srinivasa Ramanujan 22 December, 1887 Thursday 26 April, 1920 Monday 11813\n" ] } ], "source": [ "print(f\"%{max_fname}s %{max_lname}s %18s %9s %18s %9s %8d\" % (a,b,c,d,e,f,g))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Srinivasa Ramanujan 22 December, 1887 Thursday 26 April, 1920 Monday 11813\n" ] } ], "source": [ "print(a,end=\"\")\n", "print(\" \"*(max_fname + 1 - len(a)),end=\"\")\n", "\n", "print(b,end=\"\")\n", "print(\" \"*(max_lname + 1 - len(b)),end=\"\")\n", "\n", "print(\"{:19s}{:10s}{:19s}{:10s}{:8d}\".format(c,d,e,f,g))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'aaaaaaaaaa'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"aa\"*5" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Srinivasa Ramanujan 22 December, 1887 Thursday 26 April, 1920 Monday 11813\n" ] } ], "source": [ "\n", "print(f'%{max_fname}s %{max_lname}s %18s %9s %18s %9s %8d' % (a,b,c,d,e,f,g))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "class Date:\n", "\t# Constructor\n", "\tdef __init__(self, m, d, y):\n", "\t\tself.month = m\n", "\t\tself.day = d\n", "\t\tself.year = y\n", "\n", "\tdef __str__(self):\n", "\t\treturn str(self.month)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Albert Einstein 3 4\n", "Donald Knuth 1 None\n", "Srinivasa Ramanujan 12 4\n", "Isaac Newton 12 3\n", "Tim Berners-Lee 6 None\n", "Alonzo Church 8 6\n", "Alan Turing 6 6\n", "Leonhard Euler 4 9\n", "Jimi Hendrix 11 9\n", "Bob Marley 2 5\n" ] } ], "source": [ "fname = \"p1.dat\"\n", "\n", "with open(fname) as f:\n", " data = f.read().splitlines()\n", "celebrities = []\n", "for d in data: # d = 'Albert:Einstein:3/14/1879:4/18/1955'\n", " xs = d.split(\":\") # xs = ['Albert','Einstein','3/14/1879','4/18/1955']\n", " ys = xs[2].split(\"/\") # ys = ['3','14','1879']\n", " dob = Date(int(ys[0]),int(ys[1]),int(ys[2]))\n", " if xs[3] == \"\":\n", " dod = None\n", " else:\n", " ys = xs[3].split(\"/\") # ys = ['4','18','1955']\n", " dod = Date(int(ys[0]),int(ys[1]),int(ys[2]))\n", " celebrities.append((xs[0],xs[1],dob,dod))\n", "# return celebrities\n", "for celebrity in celebrities:\n", " print(celebrity[0],celebrity[1],str(celebrity[2]),str(celebrity[3]))\n" ] }, { "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.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }