.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/print_function.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basics_print_function.py: ============ 1.11 print ============ .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 10-15 ``print`` is a function in python 3. In general it will print any value given to it as inside argument. If we use two print statements, the next statement is printed on next line. Print function converts input to string (if it is not already a string) and adds a space at its start if it is not start of newline and puts new line at its end. .. GENERATED FROM PYTHON SOURCE LINES 17-22 .. code-block:: default name = " Ali" print(1) print('printing' + name) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 printing Ali .. GENERATED FROM PYTHON SOURCE LINES 23-25 similar thing happens withing loops. When we put a print function in loop, each print statement is printed on next line. .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: default for i in range(5): print('printing', i) .. rst-class:: sphx-glr-script-out .. code-block:: none printing 0 printing 1 printing 2 printing 3 printing 4 .. GENERATED FROM PYTHON SOURCE LINES 32-34 but what if we don't want to print at next line. We can do this by making use of `end` keyword in print function. .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: default for i in range(5): print(i, end=', ') .. rst-class:: sphx-glr-script-out .. code-block:: none 0, 1, 2, 3, 4, .. GENERATED FROM PYTHON SOURCE LINES 41-44 The default value of argument ``end`` is ``end=\n``. The print function can take more than one argument to print, and we can separate these arguments by a custom separator using the argument ``sep``. .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: default print('Iqbal was born in', 1877, sep=': ') .. rst-class:: sphx-glr-script-out .. code-block:: none Iqbal was born in: 1877 .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: default # format # We can format the output inside print statement by either ``%`` or ``format``. While `%` # can work both in python 2 and python 3, however, here only `format` is discussed. .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: default print('Allama Iqbal was born in {} in year {}'.format('Sialkot', 1877)) .. rst-class:: sphx-glr-script-out .. code-block:: none Allama Iqbal was born in Sialkot in year 1877 .. GENERATED FROM PYTHON SOURCE LINES 60-70 .. code-block:: default rivers = { "Indus ": [3180000.525, 'Meters'], "Chenab ": [760000.850001004, "Meters"], "Jhelum": [132.00001245, "KiloMeters"] } for river, paras in rivers.items(): print('River {} is {} {} long'.format(river, paras[0], paras[1])) .. rst-class:: sphx-glr-script-out .. code-block:: none River Indus is 3180000.525 Meters long River Chenab is 760000.850001004 Meters long River Jhelum is 132.00001245 KiloMeters long .. GENERATED FROM PYTHON SOURCE LINES 71-73 The number of ``{}`` must be ``<=`` number of arguments in ``format``. The ordering of arguments in ``format`` can be customized as shown below. .. GENERATED FROM PYTHON SOURCE LINES 75-85 .. code-block:: default rivers = { "Indus ": [3180000.525, 'Meters'], "Chenab ": [760000.850001004, "Meters"], "Jhelum": [132.00001245, "KiloMeters"] } for river, paras in rivers.items(): print('River {1} is {0} {2} long'.format(paras[0], river, paras[1])) .. rst-class:: sphx-glr-script-out .. code-block:: none River Indus is 3180000.525 Meters long River Chenab is 760000.850001004 Meters long River Jhelum is 132.00001245 KiloMeters long .. GENERATED FROM PYTHON SOURCE LINES 86-88 By default ``{}`` gets as much space as required by it, but we can fix the space used by a particular ``{}``. The number of spaces must be defined after ``:`` inside ``{}``. .. GENERATED FROM PYTHON SOURCE LINES 90-94 .. code-block:: default for river, paras in rivers.items(): print('River {0:20} is {1:15} {2:15} long'.format(river, paras[0], paras[1])) .. rst-class:: sphx-glr-script-out .. code-block:: none River Indus is 3180000.525 Meters long River Chenab is 760000.850001004 Meters long River Jhelum is 132.00001245 KiloMeters long .. GENERATED FROM PYTHON SOURCE LINES 95-98 To define the format of the incoming argument in ``{}``. For example we can use `f` for fractional numbers (as done below). If we don't want fractional part, we can use `d`. For right alignment, we can use ``<``. .. GENERATED FROM PYTHON SOURCE LINES 100-104 .. code-block:: default for river, paras in rivers.items(): print('River {0:<20} is {1:<15.2f} {2:<15} long'.format(river, paras[0], paras[1])) .. rst-class:: sphx-glr-script-out .. code-block:: none River Indus is 3180000.52 Meters long River Chenab is 760000.85 Meters long River Jhelum is 132.00 KiloMeters long .. GENERATED FROM PYTHON SOURCE LINES 105-106 To left align we can use ``>``. ``e`` can be used to show numbers in scientific notation. .. GENERATED FROM PYTHON SOURCE LINES 108-112 .. code-block:: default for river, paras in rivers.items(): print('River {:>20} is {:>15.3e} {:>15} long'.format(river, paras[0], paras[1])) .. rst-class:: sphx-glr-script-out .. code-block:: none River Indus is 3.180e+06 Meters long River Chenab is 7.600e+05 Meters long River Jhelum is 1.320e+02 KiloMeters long .. GENERATED FROM PYTHON SOURCE LINES 113-114 We probably would have wished to print it like following .. GENERATED FROM PYTHON SOURCE LINES 116-120 .. code-block:: default for river, paras in rivers.items(): print('River {:<10} is {:^15.1f} {:<10} long'.format(river, paras[0], paras[1])) .. rst-class:: sphx-glr-script-out .. code-block:: none River Indus is 3180000.5 Meters long River Chenab is 760000.9 Meters long River Jhelum is 132.0 KiloMeters long .. GENERATED FROM PYTHON SOURCE LINES 121-125 ``^`` is used for center alignment. We can truncate long strings as following. If we don't truncate and if incoming string in ``{}`` is larger than the space specified, then additional space will be assigned to that ``{}``. .. GENERATED FROM PYTHON SOURCE LINES 127-130 .. code-block:: default print('{:.12}'.format('Khayber Pakhtun Khwa')) .. rst-class:: sphx-glr-script-out .. code-block:: none Khayber Pakh .. GENERATED FROM PYTHON SOURCE LINES 131-133 printing list --------------- .. GENERATED FROM PYTHON SOURCE LINES 135-141 .. code-block:: default l = [8.364, 0.37, 0.09303, 7.084999, 9.46999999, 0.28600003, 0.229, 1e-06, 9.414, 0.986001, 2.153005] print(l) .. rst-class:: sphx-glr-script-out .. code-block:: none [8.364, 0.37, 0.09303, 7.084999, 9.46999999, 0.28600003, 0.229, 1e-06, 9.414, 0.986001, 2.153005] .. GENERATED FROM PYTHON SOURCE LINES 142-145 .. code-block:: default print(*l, sep=', ') .. rst-class:: sphx-glr-script-out .. code-block:: none 8.364, 0.37, 0.09303, 7.084999, 9.46999999, 0.28600003, 0.229, 1e-06, 9.414, 0.986001, 2.153005 .. GENERATED FROM PYTHON SOURCE LINES 146-147 ``*`` unpacks the list `l` .. GENERATED FROM PYTHON SOURCE LINES 149-152 .. code-block:: default for p in l: print("{:8.5f}".format(p), end=', ') .. rst-class:: sphx-glr-script-out .. code-block:: none 8.36400, 0.37000, 0.09303, 7.08500, 9.47000, 0.28600, 0.22900, 0.00000, 9.41400, 0.98600, 2.15300, .. GENERATED FROM PYTHON SOURCE LINES 153-155 dynamic printing ------------------ .. GENERATED FROM PYTHON SOURCE LINES 157-164 .. code-block:: default import time for i in range(10): print('.', end='') time.sleep(0.3) # This is just to stop the python for 0.3 seconds. .. rst-class:: sphx-glr-script-out .. code-block:: none .......... .. GENERATED FROM PYTHON SOURCE LINES 165-166 ``\r`` removes/deletes what is present on its left side. .. GENERATED FROM PYTHON SOURCE LINES 168-171 .. code-block:: default print('Salam \r alaikum') .. rst-class:: sphx-glr-script-out .. code-block:: none Salam alaikum .. GENERATED FROM PYTHON SOURCE LINES 172-173 This can be used for more dynamic printing as following .. GENERATED FROM PYTHON SOURCE LINES 175-187 .. code-block:: default a = 0 for x in range (0,10): a = a + 1 b = ("Loading" + "." * a) # `b` is printed on top of the previous line. # sys.stdout is also called when we use print function #sys.stdout.write('\r'+b) print('\r'+b, end='') time.sleep(0.3) print ('complete') .. rst-class:: sphx-glr-script-out .. code-block:: none Loading. Loading.. Loading... Loading.... Loading..... Loading...... Loading....... Loading........ Loading......... Loading..........complete .. GENERATED FROM PYTHON SOURCE LINES 188-190 Following parts of this tutorial are not really necessary and are hardly used but they are mentioned just for the sake of completion. .. GENERATED FROM PYTHON SOURCE LINES 192-196 Customizing print function -------------------------- We can also add additional features to print function if we wish, by redefining print function, though it will hardly be required. .. GENERATED FROM PYTHON SOURCE LINES 198-210 .. code-block:: default import builtins as _builtins def MyPrint(*args, **kwargs): """My custom print() function.""" # Adding new arguments to the print function signature # is probably a bad idea. # Instead consider testing if custom argument keywords # are present in kwargs _builtins.print('Customized print function') return _builtins.print(*args, **kwargs) .. GENERATED FROM PYTHON SOURCE LINES 211-214 .. code-block:: default MyPrint('Salam') .. rst-class:: sphx-glr-script-out .. code-block:: none Customized print function Salam .. GENERATED FROM PYTHON SOURCE LINES 215-217 Colored printing ----------------- .. GENERATED FROM PYTHON SOURCE LINES 219-222 .. code-block:: default print('Normal' + '\033[91m' + ' Red' + '\033[93m' + 'yellow' + '\033[94m' + ' Blue') .. rst-class:: sphx-glr-script-out .. code-block:: none Normal Redyellow Blue .. GENERATED FROM PYTHON SOURCE LINES 223-272 .. code-block:: default COLOR = { 'Bold' : "\033[1m", 'Underlined' : "\033[4m", 'ResetBold' : "\033[21m", 'ResetUnderlined' : "\033[24m", 'Default' : "\033[39m", 'Black' : "\033[30m", 'Red' : "\033[31m", 'Green' : "\033[32m", 'Yellow' : "\033[33m", 'Blue' : "\033[34m", 'Magenta' : "\033[35m", 'Cyan' : "\033[36m", 'LightGray' : "\033[37m", 'DarkGray' : "\033[90m", 'LightRed' : "\033[91m", 'LightGreen' : "\033[92m", 'LightYellow' : "\033[93m", 'LightBlue' : "\033[94m", 'LightMagenta' : "\033[95m", 'White' : "\033[97m", 'BackgroundDefault' : "\033[49m", 'BackgroundBlack' : "\033[40m", 'BackgroundRed' : "\033[41m", 'BackgroundGreen' : "\033[42m", 'BackgroundYellow' : "\033[43m", 'BackgroundBlue' : "\033[44m", 'BackgroundMagenta' : "\033[45m", 'BackgroundCyan' : "\033[46m", 'BackgroundLightGray' : "\033[47m", 'BackgroundDarkGray' : "\033[100m", 'BackgroundLightRed' : "\033[101m", 'BackgroundLightGreen' : "\033[102m", 'BackgroundLightYellow' : "\033[103m", 'BackgroundLightBlue' : "\033[104m", 'BackgroundLightMagenta' : "\033[105m", 'BackgroundLightCyan' : "\033[106m", 'BackgroundWhite' : "\033[107m", } last_color = '\033[91m' for color, cc in COLOR.items(): print(last_color + cc) last_color = color .. rst-class:: sphx-glr-script-out .. code-block:: none Bold Underlined ResetBold ResetUnderlined Default Black Red Green Yellow Blue Magenta Cyan LightGray DarkGray LightRed LightGreen LightYellow LightBlue LightMagenta White BackgroundDefault BackgroundBlack BackgroundRed BackgroundGreen BackgroundYellow BackgroundBlue BackgroundMagenta BackgroundCyan BackgroundLightGray BackgroundDarkGray BackgroundLightRed BackgroundLightGreen BackgroundLightYellow BackgroundLightBlue BackgroundLightMagenta BackgroundLightCyan .. GENERATED FROM PYTHON SOURCE LINES 273-276 .. code-block:: default print('ali') .. rst-class:: sphx-glr-script-out .. code-block:: none ali .. GENERATED FROM PYTHON SOURCE LINES 277-278 printing special characters .. GENERATED FROM PYTHON SOURCE LINES 280-283 .. code-block:: default print("\N{COPYRIGHT SIGN}") .. rst-class:: sphx-glr-script-out .. code-block:: none © .. GENERATED FROM PYTHON SOURCE LINES 284-285 A complete code for special characters can be found here [1]_ .. GENERATED FROM PYTHON SOURCE LINES 287-346 .. code-block:: default special_characters = { "NULL": "\N{NULL}", "SECTION SIGN": "\N{SECTION SIGN}", "COPYRIGHT SIGN": "\N{COPYRIGHT SIGN}", "REGISTERED SIGN": "\N{REGISTERED SIGN}", "INVERTED QUESTION MARK": "\N{LATIN CAPITAL LETTER O WITH STROKE}", "LATIN CAPITAL LETTER O WITH STROKE": "\N{LATIN CAPITAL LETTER O WITH STROKE}", "LATIN CAPITAL LETTER SCHWA": "\N{LATIN CAPITAL LETTER SCHWA}", "LATIN CAPITAL LETTER OPEN E": "\N{LATIN CAPITAL LETTER OPEN E}", "LATIN SMALL LETTER N WITH LONG RIGHT LEG": "\N{LATIN SMALL LETTER N WITH LONG RIGHT LEG}", "LATIN CAPITAL LETTER O WITH MIDDLE TILDE": "\N{LATIN CAPITAL LETTER O WITH MIDDLE TILDE}", "LATIN CAPITAL LETTER ESH": "\N{LATIN CAPITAL LETTER ESH}", "LATIN SMALL LETTER T WITH HOOK": "\N{LATIN SMALL LETTER T WITH HOOK}", "LATIN CAPITAL LETTER T WITH RETROFLEX HOOK": "\N{LATIN CAPITAL LETTER T WITH RETROFLEX HOOK}", "LATIN CAPITAL LETTER UPSILON": "\N{LATIN CAPITAL LETTER UPSILON}", "LATIN SMALL LETTER SCHWA": "\N{LATIN SMALL LETTER SCHWA}", "LATIN SMALL LETTER ESH": "\N{LATIN SMALL LETTER ESH}", "LATIN LETTER BILABIAL CLICK": "\N{LATIN LETTER BILABIAL CLICK}", "GREEK CAPITAL LETTER OMEGA WITH TONOS": "\N{GREEK CAPITAL LETTER OMEGA WITH TONOS}", "GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS": "\N{GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS}", "GREEK CAPITAL LETTER DELTA": "\N{GREEK CAPITAL LETTER DELTA}", "GREEK CAPITAL LETTER THETA": "\N{GREEK CAPITAL LETTER THETA}", "GREEK CAPITAL LETTER LAMDA": "\N{GREEK CAPITAL LETTER LAMDA}", "GREEK CAPITAL LETTER PI": "\N{GREEK CAPITAL LETTER PI}", "GREEK CAPITAL LETTER SIGMA": "\N{GREEK CAPITAL LETTER SIGMA}", "GREEK CAPITAL LETTER PHI": "\N{GREEK CAPITAL LETTER PHI}", "GREEK CAPITAL LETTER PSI": "\N{GREEK CAPITAL LETTER PSI}", "GREEK CAPITAL LETTER OMEGA": "\N{GREEK CAPITAL LETTER OMEGA}", "GREEK SMALL LETTER ETA WITH TONOS": "\N{GREEK SMALL LETTER ETA WITH TONOS}", "GREEK SMALL LETTER IOTA WITH TONOS": "\N{GREEK SMALL LETTER IOTA WITH TONOS}", "GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS": "\N{GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS}", "GREEK SMALL LETTER ALPHA": "\N{GREEK SMALL LETTER ALPHA}", "GREEK SMALL LETTER BETA": "\N{GREEK SMALL LETTER BETA}", "GREEK SMALL LETTER GAMMA": "\N{GREEK SMALL LETTER GAMMA}", "GREEK SMALL LETTER DELTA": "\N{GREEK SMALL LETTER DELTA}", "GREEK SMALL LETTER EPSILON": "\N{GREEK SMALL LETTER EPSILON}", "GREEK SMALL LETTER ZETA": "\N{GREEK SMALL LETTER ZETA}", "GREEK SMALL LETTER ETA": "\N{GREEK SMALL LETTER ETA}", "GREEK SMALL LETTER THETA": "\N{GREEK SMALL LETTER THETA}", "GREEK SMALL LETTER LAMDA": "\N{GREEK SMALL LETTER LAMDA}", "GREEK SMALL LETTER MU": "\N{GREEK SMALL LETTER MU}", "GREEK SMALL LETTER NU": "\N{GREEK SMALL LETTER NU}", "GREEK SMALL LETTER XI": "\N{GREEK SMALL LETTER XI}", "GREEK SMALL LETTER PI": "\N{GREEK SMALL LETTER PI}", "GREEK SMALL LETTER SIGMA": "\N{GREEK SMALL LETTER SIGMA}", "GREEK SMALL LETTER TAU": "\N{GREEK SMALL LETTER TAU}", "GREEK SMALL LETTER CHI": "\N{GREEK SMALL LETTER CHI}", "GREEK SMALL LETTER PSI": "\N{GREEK SMALL LETTER PSI}", "GREEK SMALL LETTER OMEGA": "\N{GREEK SMALL LETTER OMEGA}", "GREEK PHI SYMBOL": "\N{GREEK PHI SYMBOL}", "GREEK SMALL LETTER ARCHAIC KOPPA": "\N{GREEK SMALL LETTER ARCHAIC KOPPA}", "GREEK CAPITAL THETA SYMBOL": "\N{GREEK CAPITAL THETA SYMBOL}", "GREEK LUNATE EPSILON SYMBOL" : "\N{GREEK LUNATE EPSILON SYMBOL}" } for sp, val in special_characters.items(): print("{:52} {:>5}".format(sp, val)) .. rst-class:: sphx-glr-script-out .. code-block:: none NULL SECTION SIGN § COPYRIGHT SIGN © REGISTERED SIGN ® INVERTED QUESTION MARK Ø LATIN CAPITAL LETTER O WITH STROKE Ø LATIN CAPITAL LETTER SCHWA Ə LATIN CAPITAL LETTER OPEN E Ɛ LATIN SMALL LETTER N WITH LONG RIGHT LEG ƞ LATIN CAPITAL LETTER O WITH MIDDLE TILDE Ɵ LATIN CAPITAL LETTER ESH Ʃ LATIN SMALL LETTER T WITH HOOK ƭ LATIN CAPITAL LETTER T WITH RETROFLEX HOOK Ʈ LATIN CAPITAL LETTER UPSILON Ʊ LATIN SMALL LETTER SCHWA ə LATIN SMALL LETTER ESH ʃ LATIN LETTER BILABIAL CLICK ʘ GREEK CAPITAL LETTER OMEGA WITH TONOS Ώ GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS ΐ GREEK CAPITAL LETTER DELTA Δ GREEK CAPITAL LETTER THETA Θ GREEK CAPITAL LETTER LAMDA Λ GREEK CAPITAL LETTER PI Π GREEK CAPITAL LETTER SIGMA Σ GREEK CAPITAL LETTER PHI Φ GREEK CAPITAL LETTER PSI Ψ GREEK CAPITAL LETTER OMEGA Ω GREEK SMALL LETTER ETA WITH TONOS ή GREEK SMALL LETTER IOTA WITH TONOS ί GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS ΰ GREEK SMALL LETTER ALPHA α GREEK SMALL LETTER BETA β GREEK SMALL LETTER GAMMA γ GREEK SMALL LETTER DELTA δ GREEK SMALL LETTER EPSILON ε GREEK SMALL LETTER ZETA ζ GREEK SMALL LETTER ETA η GREEK SMALL LETTER THETA θ GREEK SMALL LETTER LAMDA λ GREEK SMALL LETTER MU μ GREEK SMALL LETTER NU ν GREEK SMALL LETTER XI ξ GREEK SMALL LETTER PI π GREEK SMALL LETTER SIGMA σ GREEK SMALL LETTER TAU τ GREEK SMALL LETTER CHI χ GREEK SMALL LETTER PSI ψ GREEK SMALL LETTER OMEGA ω GREEK PHI SYMBOL ϕ GREEK SMALL LETTER ARCHAIC KOPPA ϙ GREEK CAPITAL THETA SYMBOL ϴ GREEK LUNATE EPSILON SYMBOL ϵ .. GENERATED FROM PYTHON SOURCE LINES 347-347 .. [1] ``_ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 6.021 seconds) .. _sphx_glr_download_auto_examples_basics_print_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/basics/print_function.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: print_function.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: print_function.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_