Saturday 5 December 2015

Python: Primary secondary prompts

When you open python interpreter from command line, it is said to be in interactive mode. In this mode, python opens with the primary prompt, usually three greater-than signs (">>> ").  Continuation lines it prompts with the secondary prompt, by default three dots ("... ").

$ python3.5
Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> for i in [2, 3, 5, 7]:
...     print(i)
... 
2
3
5
7


Observe above snippet, When you open python from terminal, it enters into interactive mode and prompts for the next command with the primary prompt (>>>). '...' Represents secondary prompt.

By using 'sys' module, you can change primary and secondary prompts. The variables sys.ps1 and sys.ps2 define the strings used as primary and secondary prompts.

>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> 
>>> sys.ps1='hari>'
hari>
hari>sys.ps2='next>'
hari>
hari>for i in [2, 3, 5, 7]:
next>   print(i)
next>
2
3
5
7




Previous                                                 Next                                                 Home

No comments:

Post a Comment