{"id":117898,"date":"2020-09-12T08:36:43","date_gmt":"2020-09-12T08:36:43","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=12963"},"modified":"2020-09-12T08:36:43","modified_gmt":"2020-09-12T08:36:43","slug":"python-raw_input-vs-input","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/09\/12\/python-raw_input-vs-input\/","title":{"rendered":"Python raw_input() vs input()"},"content":{"rendered":"<p><strong>Summary:<\/strong> The key differences between <code>raw_input()<\/code> and <code>input()<\/code> functions are :<\/p>\n<ul>\n<li><code>raw_input()<\/code> can be used only in Python 2.x and is obsolete in python 3.x and above and has been renamed <code>input()<\/code><\/li>\n<li>In Python 2.x, <code>raw_input()<\/code> returns a string whereas <code>input()<\/code> returns result of an evaluation. While in Python 3.x <code>input() <\/code>returns a string but can be converted to another type like a number. <\/li>\n<\/ul>\n<h2>Overview<\/h2>\n<p>Before looking at the differences between <code>raw_input()<\/code> and <code>input()<\/code>, let us understand why we need them?<\/p>\n<p>A user-friendly code is one that is interactive. To make a code interactive instead of hard coding values, a developer\/programmer must aim to allow the user to input their own values into the program. We use the <code>raw_input()<\/code> and<code> input()<\/code> functions to accept user inputs.<\/p>\n<p><strong>Example:<\/strong> The following program is an example to accept user input in Python:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">name = input(\"Please enter your full name: \")\nage = input(\"Please enter your age: \")\n# In Python2.x use raw_input() instead print(\"Name: \", name)\nprint(\"Age: \", age)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Please enter your full name: FINXTER\nPlease enter your age: 25\nName: FINXTER\nAge: 25<\/pre>\n<p>In this article, we shall be discussing the key differences between the<code> input()<\/code> and <code>raw_input()<\/code> functions. So let us jump into the mission-critical question:<\/p>\n<p><strong>Problem:<\/strong> What is difference between <code>raw_input()<\/code> and <code>input()<\/code> in python?<\/p>\n<p>Let us have an in-depth look at each difference one by one:<\/p>\n<h2>Existential Difference<\/h2>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>raw_input()<\/strong><\/td>\n<td><strong>input()<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Inbuilt function present only in Python 2.x and is not a part of Python 3.x<\/td>\n<td>Inbuilt function present in both, Python 2.x and Python 3.x<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2>Functional Difference Based on <a href=\"https:\/\/blog.finxter.com\/check-python-version-in-your-script-a-helpful-illustrated-guide\/\">Python Versions<\/a><\/h2>\n<figure class=\"wp-block-table is-style-regular\">\n<table class=\"has-subtle-pale-blue-background-color has-background\">\n<tbody>\n<tr>\n<td><strong>&nbsp;<\/strong><\/td>\n<td><strong>Python 2.x<\/strong><\/td>\n<td><strong>Python 3.x<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>raw_input()<\/strong> <\/td>\n<td>\u25c6 <strong><code>raw_input()<\/code> <\/strong>accepts input as it is, i.e. exactly as the input has been entered by the user and returns a string.<\/p>\n<p>\u25c6 Since it accepts the input as it is, it does not expect the input to be syntactically correct. \u00a0<\/td>\n<td>\u25c6 <strong><code>raw_input()<\/code><\/strong> is obsolete and no longer a part of Python 3.x and above.<\/td>\n<\/tr>\n<tr>\n<td><strong>input()<\/strong><\/td>\n<td>\u25c6<code> <\/code><strong>input()<\/strong> accepts the input from the user as a statement or expression and returns the output after evaluating the input. In other words, it accepts the user entry as raw_input(), performs an eval() on it, and then returns the result as output. <\/p>\n<p>\u25c6\u00a0It expects a syntactically correct input (statement\/expression) from the user.<\/td>\n<td>\u25c6 In Python 3.x, <code>raw_input()<\/code> has been replaced by<code> <\/code><strong>input()<\/strong>. This means that the input() function performs the same operation in Python 3.x as <code>raw_input()<\/code> used to do in Python 2. \u00a0<\/p>\n<p>Thus <code>input()<\/code> accepts and returns a string in Python 3.x and above. \u00a0\u00a0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2>Examples<\/h2>\n<p class=\"has-vivid-red-color has-text-color\"><strong>Python 2.x<\/strong><\/p>\n<p>\u270e<strong> input() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = raw_input(\"What is your name? \")\nprint \"Name: %s\" %a)\nb = raw_input(\" Enter a mathematical expression: \")\nprint Output\": %d\", %b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">What is your name? Finxter\nName: Finxter Enter a mathematical expression: 2+5\nOutput: 2+5<\/pre>\n<p class=\"has-vivid-red-color has-text-color\">\u270e <strong>raw_input() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = input(\"Enter Your Full Name: \")\nprint \"Name: %s \" %a\nb = input(\"Enter a Mathematical Expression: \")\nprint \"Output: %d\" %b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Enter Your Full Name: 'Finxter Shubham'\nName: Finxter Shubham\nEnter a Mathematical Expression: 5**2\nOutput: 25<\/pre>\n<p class=\"has-vivid-cyan-blue-color has-text-color\"><strong>Python 3.x<\/strong> <strong>And Above<\/strong><\/p>\n<p>\u270e<strong> input() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = input(\"What is your name? \")\nprint(\"Name: \", a)\nb = input(\"Enter a mathematical expression: \")\nprint(\"Output: \", b)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">What is your name? Finxter Shubham\nName: Finxter Shubham\nEnter a mathematical expression: 3+5\nOutput: 3+5<\/pre>\n<h2>Trivia<\/h2>\n<p>If you want to implement or leverage the functionality of <code>input()<\/code> of Python 2.x in Python 3.x and evaluate the statement entered by the user, you can use one of the following procedures:<\/p>\n<ul>\n<li>Type Conversion : int(input(\u201cEnter value\u201d))<\/li>\n<li>Using eval(input(\u201cEnter Value\u201d))<\/li>\n<\/ul>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = int(input(\"Enter first number: \"))\nb = int(input(\"Enter second number: \"))\nprint(\"Addition: \", a+b)\nx = eval(input(\"Enter a mathematical expression: \"))\nprint(\"Result: \", x)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Enter first number: 25\nEnter second number: 75\nAddition: 100\nEnter a mathematical expression: 10**2\nResult: 100<\/pre>\n<p>But you must avoid the usage of <code>eval()<\/code> function unless necessary because it has a severe drawback.<\/p>\n<p>I would strongly recommend you to read <a href=\"https:\/\/blog.finxter.com\/how-to-read-inputs-as-numbers-in-python\/\">this article<\/a> in connection with this topic. It will help you have a broader understanding of this concept. Also, if you are wondering about the version of python installed in your system, you may want to have a look at<a href=\"https:\/\/blog.finxter.com\/check-python-version-in-your-script-a-helpful-illustrated-guide\/\"> this article<\/a>.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we discussed the key differences between <code>input() <\/code>and<code> raw_input()<\/code> in terms of their functionality and existence in <a href=\"https:\/\/blog.finxter.com\/check-python-version-in-your-script-a-helpful-illustrated-guide\/\">different versions of python<\/a> along with their examples. I hope all your doubts regarding the difference between <code>input()<\/code> and <code>raw_input()<\/code> have been clarified after reading this article.<\/p>\n<p><a href=\"http:\/\/blog.finxter.com\/subscribe\">Please stay tuned<\/a> <a href=\"http:\/\/blog.finxter.com\/subscribe\">and Subscribe<\/a> for more interesting articles!<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The key differences between raw_input() and input() functions are : raw_input() can be used only in Python 2.x and is obsolete in python 3.x and above and has been renamed input() In Python 2.x, raw_input() returns a string whereas input() returns result of an evaluation. While in Python 3.x input() returns a string but [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-117898","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/117898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=117898"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/117898\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=117898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=117898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=117898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}