{"id":2498,"date":"2020-01-12T13:04:55","date_gmt":"2020-01-12T13:04:55","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2498"},"modified":"2022-08-06T13:10:19","modified_gmt":"2022-08-06T13:10:19","slug":"python-input-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-input-function","title":{"rendered":"Python input() function"},"content":{"rendered":"\n<p>It often happens that the developers\/programmers need to accept the input from the user to process further with the application.<\/p>\n\n\n\n<p>Python provides a built-in-method to accept the input from the user.<\/p>\n\n\n\n<p> <strong>Python input() function<\/strong> accepts the input from the user. Python&#8217;s <code><strong>builtins.py<\/strong><\/code> contains the Python input() function. <\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input(prompt)<\/code><\/pre>\n\n\n\n<p>The prompt is optional in the input() function. Prompt is basically a String i.e a message displayed before the input.<\/p>\n\n\n\n<p>Whatever string is placed as an argument in the input function, gets printed as it is on the output screen.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Working of Python input() function<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>As soon as the interpreter encounters the input() function,  it halts\/stops the program execution until and unless the user provides an input to the program.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>The input entered by the user is automatically converted into a <a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-functions\" class=\"rank-math-link\">String<\/a>. Whether the user enters an integer or float type data, still the input() functions converts it into a string. <\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>Thus, it needs to be explicitly converted to another data type in the program using <strong><em>typecasting<\/em><\/strong>.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Example 1: Basic working of Python input() function<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = input () \nprint(&quot;The entered value is:&quot;, x)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\nThe entered value is: 10<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Python input() function with String as an argument<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nname = input(&#039;Enter your name: &#039;)\nprint(&quot;Name of the candidate is:&quot;, name)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter your name: Safa Mulani\nName of the candidate is: Safa Mulani<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Multiply two numbers by accepting input from the user<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = input()\n\ny = input()\n\nz = x * y\nprint(&quot;Result is: &quot;, z)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1110\" height=\"179\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/input-function.png\" alt=\"Input Function\" class=\"wp-image-2502\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/input-function.png 1110w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/input-function-300x48.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/input-function-1024x165.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/input-function-768x124.png 768w\" sizes=\"auto, (max-width: 1110px) 100vw, 1110px\" \/><figcaption><em><strong>Python input() Function<\/strong><\/em><\/figcaption><\/figure>\n\n\n\n<p>The above code snippet results in an error because the python input() function converts the input from the user into a string. Thus, in order to convert it into an integer, we need to explicitly typecast the input provided by the user.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = input(&quot;Enter the number1: &quot;)\n\n\ny = input(&quot;Enter the number2: &quot;)\n\nz = int(x) * int(y)\nprint(&quot;Result is: &quot;, z)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the number1: 10\nEnter the number2: 20\nResult is:  200<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the working of Python input() function.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python input() function<\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#input\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">input() function Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>It often happens that the developers\/programmers need to accept the input from the user to process further with the application. Python provides a built-in-method to accept the input from the user. Python input() function accepts the input from the user. Python&#8217;s builtins.py contains the Python input() function. Syntax: The prompt is optional in the input() [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2498","post","type-post","status-publish","format-standard","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2498","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2498"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2498\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}