{"id":381,"date":"2019-07-08T10:36:56","date_gmt":"2019-07-08T10:36:56","guid":{"rendered":"http:\/\/askpython.com\/?p=381"},"modified":"2024-01-16T02:12:27","modified_gmt":"2024-01-16T02:12:27","slug":"python-user-input","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python-user-input","title":{"rendered":"Python User Input from Keyboard &#8211; input() function"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>Python user input from the keyboard can be read using the input() built-in function.<\/li>\n\n\n\n<li>The input from the user is read as a string and can be assigned to a variable.<\/li>\n\n\n\n<li>After entering the value from the keyboard, we have to press the &#8220;Enter&#8221; button. Then the input() function reads the value entered by the user.<\/li>\n\n\n\n<li>The program halts indefinitely for the user input. There is no option to provide timeout value.<\/li>\n\n\n\n<li>If we enter EOF <em><strong>(*nix: Ctrl-D, Windows: Ctrl-Z+Return)<\/strong><\/em>, EOFError is raised and the program is terminated.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of input() Function<\/h2>\n\n\n\n<p>The syntax of input() function is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput(prompt)\n<\/pre><\/div>\n\n\n<p>The prompt string is printed on the console and the control is given to the user to enter the value. You should print some useful information to guide the user to enter the expected value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Getting User Input in Python<\/h2>\n\n\n\n<p>Here is a simple example of getting the user input and printing it on the console.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nvalue = input(&quot;Please enter a string:\\n&quot;)\n\nprint(f&#039;You entered {value}&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"772\" height=\"464\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input.png\" alt=\"Python User Input\" class=\"wp-image-382\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input.png 772w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-300x180.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-768x462.png 768w\" sizes=\"auto, (max-width: 772px) 100vw, 772px\" \/><figcaption class=\"wp-element-caption\">Python User Input<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is the type of user entered value?<\/h2>\n\n\n\n<p>The user entered value is always converted to a string and then assigned to the variable. Let&#8217;s confirm this by using type() <a rel=\"noreferrer noopener\" label=\"function (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/python-functions\" target=\"_blank\">function<\/a> to get the type of the input <a href=\"https:\/\/www.askpython.com\/python\/python-variables\" target=\"_blank\" rel=\"noreferrer noopener\" label=\"variable (opens in a new tab)\">variable<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nvalue = input(&quot;Please enter a string:\\n&quot;)\n\nprint(f&#039;You entered {value} and its type is {type(value)}&#039;)\n\nvalue = input(&quot;Please enter an integer:\\n&quot;)\n\nprint(f&#039;You entered {value} and its type is {type(value)}&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPlease enter a string:\nPython\nYou entered Python and its type is &lt;class &#039;str&#039;&gt;\nPlease enter an integer:\n123\nYou entered 123 and its type is &lt;class &#039;str&#039;&gt;\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How to get an Integer as the User Input?<\/h2>\n\n\n\n<p>There is no way to get an integer or any other type as the user input. However, we can use the built-in functions to convert the entered string to the integer.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nvalue = input(&quot;Please enter an integer:\\n&quot;)\n\nvalue = int(value)\n\nprint(f&#039;You entered {value} and its square is {value ** 2}&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"974\" height=\"514\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-integer.png\" alt=\"Python User Input Integer\" class=\"wp-image-383\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-integer.png 974w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-integer-300x158.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-integer-768x405.png 768w\" sizes=\"auto, (max-width: 974px) 100vw, 974px\" \/><figcaption class=\"wp-element-caption\">Python User Input Integer<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python user input and EOFError Example<\/h2>\n\n\n\n<p>When we enter EOF, input() raises EOFError and terminates the program. Let&#8217;s look at a simple example using PyCharm IDE.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nvalue = input(&quot;Please enter an integer:\\n&quot;)\n\nprint(f&#039;You entered {value}&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPlease enter an integer:\n^D\nTraceback (most recent call last):\n  File &quot;\/Users\/pankaj\/Documents\/PycharmProjects\/PythonTutorialPro\/hello-world\/user_input.py&quot;, line 1, in &lt;module&gt;\n    value = input(&quot;Please enter an integer:\\n&quot;)\nEOFError: EOF when reading a line\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"324\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-EOFError-1024x324.png\" alt=\"Python User Input EOFError\" class=\"wp-image-384\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-EOFError-1024x324.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-EOFError-300x95.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-EOFError-768x243.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-EOFError-1536x485.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-EOFError.png 1734w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Python User Input raises EOFError<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python User Input Choice Example<\/h2>\n\n\n\n<p>We can build an intelligent system by giving choice to the user and taking the user input to proceed with the choice.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nvalue1 = input(&quot;Please enter first integer:\\n&quot;)\nvalue2 = input(&quot;Please enter second integer:\\n&quot;)\n\nv1 = int(value1)\nv2 = int(value2)\n\nchoice = input(&quot;Enter 1 for addition.\\nEnter 2 for subtraction.\\nEnter 3 for Multiplication.:\\n&quot;)\nchoice = int(choice)\n\nif choice == 1:\n    print(f&#039;You entered {v1} and {v2} and their addition is {v1 + v2}&#039;)\nelif choice == 2:\n    print(f&#039;You entered {v1} and {v2} and their subtraction is {v1 - v2}&#039;)\nelif choice == 3:\n    print(f&#039;You entered {v1} and {v2} and their multiplication is {v1 * v2}&#039;)\nelse:\n    print(&quot;Wrong Choice, terminating the program.&quot;)\n<\/pre><\/div>\n\n\n<p>Here is a sample output from the execution of the above program.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"699\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-choice-1024x699.png\" alt=\"Python User Input Choice\" class=\"wp-image-387\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-choice-1024x699.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-choice-300x205.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-choice-768x525.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-user-input-choice.png 1502w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Python User Input Choice<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Quick word on Python raw_input() function<\/h2>\n\n\n\n<p>The raw_input() function was used to take user input in Python 2.x versions. Here is a simple example from Python 2.7 command line interpreter showing the use of raw_input() function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n~ python2.7\nPython 2.7.10 (default, Feb 22 2019, 21:55:15) \n&#x5B;GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n&gt;&gt;&gt; value = raw_input(&quot;Please enter a string\\n&quot;)\nPlease enter a string\nHello\n&gt;&gt;&gt; print value\nHello\n<\/pre><\/div>\n\n\n<p>This function has been deprecated and removed from Python 3. If you are still on Python 2.x versions, it&#8217;s recommended to upgrade to Python 3.x versions.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>It&#8217;s very easy to take the user input in Python from the input() function. It&#8217;s mostly used to provide choice of operation to the user and then alter the flow of the program accordingly.<\/p>\n\n\n\n<p>However, the program waits indefinitely for the user input. It would have been nice to have some timeout and default value in case the user doesn&#8217;t enter the value in time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3.8\/library\/functions.html#input\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Python.org input() documentation (opens in a new tab)\">Python.org input() documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Syntax of input() Function The syntax of input() function is: The prompt string is printed on the console and the control is given to the user to enter the value. You should print some useful information to guide the user to enter the expected value. Getting User Input in Python Here is a simple example [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-381","post","type-post","status-publish","format-standard","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}