{"id":5754,"date":"2022-02-19T19:25:08","date_gmt":"2022-02-19T18:25:08","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5754"},"modified":"2026-01-24T14:13:37","modified_gmt":"2026-01-24T13:13:37","slug":"input-function","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/02\/19\/input-function\/","title":{"rendered":"Input() function"},"content":{"rendered":"<p>1. The <code>print()<\/code> function <strong>sends data to the console<\/strong>, while the <code>input()<\/code> function <strong>gets data from the console<\/strong>.<\/p>\n<p><!--more--><\/p>\n<p>2. The <code>input()<\/code> function comes with an optional parameter: <strong>the prompt string<\/strong>. It allows you to write a message before the user input, e.g.:<\/p>\n<pre class=\"lang:default decode:true\">name = input(\"Enter your name: \")\r\nprint(\"Hello, \" + name + \". Nice to meet you!\")<\/pre>\n<p>&nbsp;<\/p>\n<p>3. When the <code>input()<\/code> function is called, the program&#8217;s flow is stopped, the prompt symbol keeps blinking (it prompts the user to take action when the console is switched to input mode) until the user has entered an input and\/or pressed the <i>Enter<\/i> key.<\/p>\n<p>&nbsp;<\/p>\n<p><em><span class=\"label\">NOTE<\/span><\/em><\/p>\n<p>The <code>input()<\/code> function can be used to prompt the user to end a program. Look at the code below:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">name = input(\"Enter your name: \")\r\nprint(\"Hello, \" + name + \". Nice to meet you!\")\r\n\r\n\r\nprint(\"\\nPress Enter to end the program.\")\r\ninput()\r\nprint(\"THE END.\")<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"ace_line\">4. The result of the <code>input()<\/code> function is a string. You can add strings to each other using the concatenation (<code>+<\/code>) operator. Check out this code:<\/div>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">num_1 = input(\"Enter the first number: \") # Enter 12\r\nnum_2 = input(\"Enter the second number: \") # Enter 21\r\n\r\nprint(num_1 + num_2) # the program returns 1221<\/pre>\n<p>&nbsp;<\/p>\n<p>5. You can also multiply (<code>*<\/code> \u2012 replication) strings, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">my_input = input(\"Enter something: \") # Example input: hello\r\nprint(my_input * 3) # Expected output: hellohellohello<\/pre>\n<\/div>\n<\/div>\n<div class=\"ace_line\"><\/div>\n<div>\n<h1>Type casting<\/h1>\n<p>Python offers two simple functions to specify a type of data and solve this problem &#8211; here they are: <code><\/code><\/p>\n<pre class=\"lang:default decode:true\">int()<\/pre>\n<p>and<\/p>\n<pre class=\"lang:default decode:true \">float()<\/pre>\n<p>Their names are self-commenting:<\/p>\n<ul>\n<li>the <code>int()<\/code> function <strong>takes one argument<\/strong> (e.g., a string: <code>int(string)<\/code>) and tries to convert it into an integer; if it fails, the whole program will fail too (there is a workaround for this situation, but we&#8217;ll show you this a little later);<\/li>\n<li>the <code>float()<\/code> function takes one argument (e.g., a string: <code>float(string)<\/code>) and tries to convert it into a float (the rest is the same).<\/li>\n<\/ul>\n<h1>Type conversion: str()<\/h1>\n<p>This type of conversion is not a one-way street. You can also <strong>convert a number into a string<\/strong>, which is way easier and safer \u2012 this kind of operation is always possible.<\/p>\n<p>A function capable of doing that is called <code>str()<\/code>:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true \">str(number)<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 1<\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<p>What is the output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">x = int(input(\"Enter a number: \")) # The user enters 2\r\nprint(x * \"5\")<\/pre>\n<\/div>\n<\/div>\n<p id=\"sol\"><code class=\"codep \">55<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 2<\/em><\/p>\n<p>What is the expected output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">x = input(\"Enter a number: \") # The user enters 2\r\nprint(type(x))<\/pre>\n<\/div>\n<\/div>\n<p id=\"sol2\"><code class=\"codep \">&lt;class 'str'&gt;<\/code><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 3<\/p>\n<p>What is the output of the following snippet of the user enters two lines containing <code>2<\/code> and <code>4<\/code> respectively ?<\/p>\n<pre class=\"lang:default decode:true \">    x = input()\r\n    y = int(input())\r\n    print(x * y)<\/pre>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">A. <\/span><code>2222<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">B. <\/span>The code is erroneous.<\/div>\n<div>C. <code>2 * 4<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>D. <code>8<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><em>Explanation:<\/em><\/p>\n<p>The <code>input()<\/code> function always returns a string. So <code>x<\/code> is a string.<\/p>\n<p>The<code> int()<\/code> function converts its argument (string, float, etc&#8230;) to an int. So, <code>y<\/code> is an int.<\/p>\n<p>The <code>*<\/code> operator can be used with one string ABC and an int x : the result will be the string ABC repeated x times.<\/p>\n<p><em>Try it yourself:<\/em><\/p>\n<pre class=\"lang:default decode:true \">    x = input(\"Enter x:\")        # enter 2\r\n    print(type(x))               # &lt;class 'str'&gt;\r\n     \r\n    y = int(input(\"Enter y:\"))   # enter 4\r\n    print(type(y))               # &lt;class 'int'&gt;\r\n     \r\n    print(x * y)                 # 2222<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 4<\/p>\n<p>What is the expected output of the following code if :<\/p>\n<p>(1) the user enters <code>10\/2<\/code> when prompted ?<\/p>\n<p>(2) the user enters <code>3.1<\/code> when prompted ?<\/p>\n<pre class=\"lang:default decode:true \">    x = input(\"Enter number : \")\r\n    print(int(x))<\/pre>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>(1) : 5<br \/>\n(2) : 3<\/p>\n<p>(1) : 5<br \/>\n(2) : The code will raise an unhandled exception<\/p>\n<p>(1) : The code will raise an unhandled exception<br \/>\n(2) : 3<\/p>\n<p>(1) : The code will raise an unhandled exception<br \/>\n(2) : The code will raise an unhandled exception<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><em>Explanation:<\/em><\/p>\n<p>The <code>input()<\/code> function returns a string.<\/p>\n<p>The\u00a0 <code>int()<\/code> function returns an integer object from any number or string &#8211; if the function cannot convert the object to an integer, an exception is raised.<\/p>\n<p>Here in the above question, when the user enters <code>10\/2<\/code>, this will be treated as a string and the <code>int()<\/code> function will not be able to convert this string into an integer &#8211;&gt; an exception will be raised.<\/p>\n<p>When the user enters <code>3.1<\/code> , this is also a string : the <code>int()<\/code> function will not be able to convert this string into an integer &#8211;&gt; an exception will be raised. If this had been a float, then <code>int(3.1)<\/code> would have returned <code>3<\/code>.<\/p>\n<p><em>Try it yourself:<\/em><\/p>\n<pre class=\"lang:default decode:true \">    x = input(\"Enter number : \") # enter 10\/2\r\n    print(int(x))    # exception\r\n     \r\n    x = input(\"Enter number : \") # enter 3.1\r\n    print(int(x))    # exception\r\n     \r\n    # Below, we convert the string to a float before calling int():\r\n    x = float(input(\"Enter number : \")) # enter 3.1\r\n    print(int(x))    # 3<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. The print() function sends data to the console, while the input() function gets data from the console.<\/p>\n","protected":false},"author":2,"featured_media":5934,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5754"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5754"}],"version-history":[{"count":10,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5754\/revisions"}],"predecessor-version":[{"id":6292,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5754\/revisions\/6292"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/5934"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5754"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}