Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python raw_input() vs input()

#1
Python raw_input() vs input()

<div><p><strong>Summary:</strong> The key differences between <code>raw_input()</code> and <code>input()</code> functions are :</p>
<ul>
<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>
<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>
</ul>
<h2>Overview</h2>
<p>Before looking at the differences between <code>raw_input()</code> and <code>input()</code>, let us understand why we need them?</p>
<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>
<p><strong>Example:</strong> The following program is an example to accept user input in Python:</p>
<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: ")
age = input("Please enter your age: ")
# In Python2.x use raw_input() instead print("Name: ", name)
print("Age: ", age)</pre>
<p><strong>Output</strong></p>
<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
Please enter your age: 25
Name: FINXTER
Age: 25</pre>
<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>
<p><strong>Problem:</strong> What is difference between <code>raw_input()</code> and <code>input()</code> in python?</p>
<p>Let us have an in-depth look at each difference one by one:</p>
<h2>Existential Difference</h2>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>raw_input()</strong></td>
<td><strong>input()</strong></td>
</tr>
<tr>
<td>Inbuilt function present only in Python 2.x and is not a part of Python 3.x</td>
<td>Inbuilt function present in both, Python 2.x and Python 3.x</td>
</tr>
</tbody>
</table>
</figure>
<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>
<figure class="wp-block-table is-style-regular">
<table class="has-subtle-pale-blue-background-color has-background">
<tbody>
<tr>
<td><strong>&nbsp;</strong></td>
<td><strong>Python 2.x</strong></td>
<td><strong>Python 3.x</strong></td>
</tr>
<tr>
<td><strong>raw_input()</strong> </td>
<td>◆ <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>
<p>◆ Since it accepts the input as it is, it does not expect the input to be syntactically correct.  </td>
<td>◆ <strong><code>raw_input()</code></strong> is obsolete and no longer a part of Python 3.x and above.</td>
</tr>
<tr>
<td><strong>input()</strong></td>
<td>◆<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>
<p>◆ It expects a syntactically correct input (statement/expression) from the user.</td>
<td>◆ 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.  </p>
<p>Thus <code>input()</code> accepts and returns a string in Python 3.x and above.   </td>
</tr>
</tbody>
</table>
</figure>
<h2>Examples</h2>
<p class="has-vivid-red-color has-text-color"><strong>Python 2.x</strong></p>
<p>✎<strong> input() function</strong></p>
<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? ")
print "Name: %s" %a)
b = raw_input(" Enter a mathematical expression: ")
print Output": %d", %b</pre>
<p><strong>Output</strong></p>
<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
Name: Finxter Enter a mathematical expression: 2+5
Output: 2+5</pre>
<p class="has-vivid-red-color has-text-color">✎ <strong>raw_input() function</strong></p>
<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: ")
print "Name: %s " %a
b = input("Enter a Mathematical Expression: ")
print "Output: %d" %b</pre>
<p><strong>Output</strong></p>
<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'
Name: Finxter Shubham
Enter a Mathematical Expression: 5**2
Output: 25</pre>
<p class="has-vivid-cyan-blue-color has-text-color"><strong>Python 3.x</strong> <strong>And Above</strong></p>
<p>✎<strong> input() function</strong></p>
<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? ")
print("Name: ", a)
b = input("Enter a mathematical expression: ")
print("Output: ", b)</pre>
<p><strong>Output</strong></p>
<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
Name: Finxter Shubham
Enter a mathematical expression: 3+5
Output: 3+5</pre>
<h2>Trivia</h2>
<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>
<ul>
<li>Type Conversion : int(input(“Enter value”))</li>
<li>Using eval(input(“Enter Value”))</li>
</ul>
<p><strong>Example</strong></p>
<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: "))
b = int(input("Enter second number: "))
print("Addition: ", a+b)
x = eval(input("Enter a mathematical expression: "))
print("Result: ", x)</pre>
<p><strong>Output:</strong></p>
<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
Enter second number: 75
Addition: 100
Enter a mathematical expression: 10**2
Result: 100</pre>
<p>But you must avoid the usage of <code>eval()</code> function unless necessary because it has a severe drawback.</p>
<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>
<h2>Conclusion</h2>
<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>
<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>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/09/...-vs-input/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016