{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Print Statement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **print** statement can be used in the following different ways :\n", "\n", " - print \"Hello World\"\n", " - print \"Hello\", \n", " - print \"Hello\" + \n", " - print \"Hello %s\" % " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print \"Hello World\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python, single, double and triple quotes are used to denote a string.\n", "Most use single quotes when declaring a single character. \n", "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hey\n" ] } ], "source": [ "print 'Hey'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Rajath Kumar M.P.\n", "\n", "I love Python.\n" ] } ], "source": [ "print \"\"\"My name is Rajath Kumar M.P.\n", "\n", "I love Python.\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n", "Hello World !\n" ] } ], "source": [ "string1 = 'World'\n", "print 'Hello', string1\n", "\n", "string2 = '!'\n", "print 'Hello', string1, string2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HelloWorld!\n" ] } ], "source": [ "print 'Hello' + string1 + string2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**%s** is used to refer to a variable which contains a string." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print \"Hello %s\" % string1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, when using other data types\n", "\n", " - %s -> string\n", " - %d -> Integer\n", " - %f -> Float\n", " - %o -> Octal\n", " - %x -> Hexadecimal\n", " - %e -> exponential\n", " \n", "This can be used for conversions inside the print statement itself." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Actual Number = 18\n", "Float of the number = 18.000000\n", "Octal equivalent of the number = 22\n", "Hexadecimal equivalent of the number = 12\n", "Exponential equivalent of the number = 1.800000e+01\n" ] } ], "source": [ "print \"Actual Number = %d\" %18\n", "print \"Float of the number = %f\" %18\n", "print \"Octal equivalent of the number = %o\" %18\n", "print \"Hexadecimal equivalent of the number = %x\" %18\n", "print \"Exponential equivalent of the number = %e\" %18" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When referring to multiple variables parenthesis is used." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World !\n" ] } ], "source": [ "print \"Hello %s %s\" %(string1,string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Other Examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following are other different ways the print statement can be put to use." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I want %d to be printed here\n" ] } ], "source": [ "print \"I want %%d to be printed %s\" %'here'" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_A_A_A_A_A_A_A_A_A_A\n" ] } ], "source": [ "print '_A'*10" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Jan\n", "Feb\n", "Mar\n", "Apr\n", "May\n", "Jun\n", "Jul\n", "Aug\n" ] } ], "source": [ "print \"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I want \\n to be printed.\n" ] } ], "source": [ "print \"I want \\\\n to be printed.\"" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Routine:\n", "\t- Eat\n", "\t- Sleep\n", "\t- Repeat\n", "\n" ] } ], "source": [ "print \"\"\"\n", "Routine:\n", "\\t- Eat\n", "\\t- Sleep\\n\\t- Repeat\n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#PrecisionWidth and FieldWidth" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n", "\n", "The default Precision Width is set to 6." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.121312'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.12131'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' 3.12131'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%9.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zero padding is done by adding a 0 at the start of fieldwidth." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'00000000000003.12131'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%020.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 3.121312\n", "-3.121312\n" ] } ], "source": [ "print \"% 9f\" % 3.121312312312\n", "print \"% 9f\" % -3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+3.121312\n", "-3.121312\n" ] } ], "source": [ "print \"%+9f\" % 3.121312312312\n", "print \"% 9f\" % -3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actualy field width. But left alignment can be done by specifying a negative symbol in the field width." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.121 '" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%-9.3f\" % 3.121312312312" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }