Python Input Output (I/O)

Python input output is the way communication between the computer or any other information processing system and user.

What is Input / Output ?

The input and output is the way communication between the computer or any other information processing system and user.  As we know communication is a way of interaction with someone. Computer system takes input, process result and display output. Inputs are the signals or data received by the computer, and output is signal or data produced by the computer.

Python Input Output
fig: Input Output Process

Python Input Output

Every programming language has their own syntax for taking input and producing output. For example you may see in C, there is cin for input and cout for output. Similarly, python has their own predefined functions for the same.Python uses input() function to take input and print() to display output.

Python input output process process allows us to interact with python interpreter (we can say system ). input() takes input , interpreter reads and process (interprets) that output and produces result. This result can be display to the user using print().

Python Input

Python allows us to take input from the user. It offers  two built in functions for taking input from user, these are:

input()

It is used to take input from the user. It evaluates , whatever expression given by the user. After retrieving input parser parses the source code. In simple words we can say input() takes inputed expression from the user. Here is an example of code.

Syntax

input('String to display/ statement')

Example

python input
fig 1: python input

Output

python input
fig 2 :python input

raw_input()

raw_input() function is used to take input from the user. It takes the input from the standard input in the form of a string and reads the data from a line at once. raw_input function returns a string. Hence in case an expression is to be evaluated, then it has to be typecast to its following data type.

Syntax

raw_input('statement')

Example

python input
fig 3: python input

Output

python input
fig 4: python input

As you can see above raw_input() takes data as a string , so it concatenate string rather than adding integers. In case of using input data as other data type we need to type casting into that data-type. Suppose here we need integer type of data, so it should be:

x =int( raw_input("Enter number one"))
y = int(raw_input("Enter number two"))
print(x+y)

It will produce result as fig 2 :python input.

Difference between input() and raw_input()

In python version 2.x , the main difference between  input() and raw_input() is that,  input() first takes the raw_input and then convert it into an expression using eval().

In other hand, raw_input() takes  exactly what user typed and then pass it back to string. There is need to type casting (if other type of data needed).

In python version 3.x there is no raw_input . Python 3.x was renamed raw_input to input() and removed the older input().

The main difference is that input() contains the syntactically correct python statements where raw_input() does not.

Python eval()

Python eval() function converts the string data into python expression. Mostly in is used with input(). It converts the user input string  into  python expression and gives result.

Syntax

eval(?arguments?)

Example

eval('10+10')

Output

20

print()

The print() function prints the given object to the standard output device (screen) or to the text stream file.As you can see in above figures, print() statement displays result to the screen. It can take a variable, string or other object as an argument and displays it. Print statement returns none.

Syntax

  • To print single object
print(?arg1)
  • To print multiple objects
print(?arg1, arg2,..., argn)
  • for concatenate srings
print('str1'+'str2')

String formatting using print()

Python allows us to add variables into string using string formatting. Put set of curly braces( {} ) , where you need to add variable data into string. .format() function is used for the same. We pass data as a parameters to the format method.

Syntax

print("String {} {}...{}".format(var1,var2,..varn))

Example

string formatting
string formatting

Output

string formatting
string formatting

So it is more than enough to understand python input output and string formatting. Thanks.

40 thoughts on “Python Input Output (I/O)

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Awesome blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your theme. Appreciate it

  2. You really make it seem so easy with your presentation but I find this topic to be actually one thing that I believe I would never understand. It seems too complicated and very wide for me. I am having a look forward to your subsequent submit, I will try to get the cling of it!

  3. Just wish to say your article is as amazing. The clarity to your post is just spectacular and that i could assume you’re knowledgeable on this subject. Fine with your permission allow me to grasp your RSS feed to stay updated with forthcoming post. Thanks one million and please carry on the gratifying work.

  4. I get joy via, result in I discovered exactly what I’d been looking intended for. You may have ended our 5 day time extended quest! God Many thanks gentleman. Have a very good day time aplikasi berita saham terkini. L8rs

  5. I¡¦m not sure where you are getting your info, however great topic. I needs to spend a while finding out more or working out more. Thanks for fantastic info I used to be looking for this information for my mission.

  6. I precisely desired to appreciate you once more. I’m not certain what I would have undertaken without the actual advice shared by you over that problem. It previously was a very depressing difficulty in my position, but understanding your specialised tactic you resolved it took me to jump for contentment. I am just grateful for the assistance and thus hope you find out what an amazing job you have been getting into teaching men and women through the use of your webblog. Most probably you’ve never come across all of us.

  7. certainly like your web-site but you need to take a look at the spelling on quite a few of your posts. A number of them are rife with spelling issues and I find it very bothersome to tell the reality nevertheless I¡¦ll definitely come back again.

  8. Thanks for sharing excellent informations. Your site is so cool. I’m impressed by the details that you have on this website. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my pal, ROCK! I found simply the information I already searched all over the place and just couldn’t come across. What a great website.

  9. Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Actually Wonderful. I’m also a specialist in this topic so I can understand your hard work.

  10. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but instead of that, this is fantastic blog. A fantastic read. I’ll certainly be back.

  11. Nicely, that is certainly good, nonetheless how about further alternatives we’ve here? Can you mind composing yet another article relating to them all furthermore? Appreciate it!

  12. I just want to tell you that I’m very new to weblog and honestly loved you’re web site. Almost certainly I’m planning to bookmark your site . You actually come with impressive article content. Thanks for sharing your web-site.

  13. I’ve been exploring for a little for any high-quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this website. Reading this information So i’m happy to convey that I’ve a very good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to don’t forget this site and give it a look on a constant basis.

  14. I am extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you modify it yourself? Either way keep up the excellent quality writing, it is rare to see a great blog like this one nowadays..

  15. This is very interesting, You are a very skilled blogger. I’ve joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your site in my social networks!

  16. Hi there very nice blog!! Man .. Beautiful .. Superb .. I’ll bookmark your site and take the feeds also…I am satisfied to find so many useful information here within the publish, we’d like work out more techniques on this regard, thanks for sharing. . . . . .

  17. Somebody essentially help to make seriously posts I might state. That is the first time I frequented your website page and so far? I surprised with the research you made to create this particular publish incredible. Great job!

  18. I have been exploring for a little bit for any high-quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this website. Reading this information So i am happy to convey that I have a very good uncanny feeling I discovered just what I needed. I most certainly will make sure to do not forget this web site and give it a look on a constant basis.

  19. Hey There. I found your blog using msn. This is a really well written article.

    I will make sure to bookmark it and return to read more of your useful information. Thanks for the post.
    I’ll definitely comeback.

  20. I really do accept all the aspects you may have introduced within your write-up. They can be incredibly begging and can undoubtedly get the job done. Even now, a discussions are certainly fast for newbies.. video youtube terfavorit May you please increase them a little bit through the next occasion? Basically submit.

  21. My developer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am anxious about switching to another platform. I have heard excellent things about blogengine.net. Is there a way I can import all my wordpress content into it? Any kind of help would be greatly appreciated!

  22. hey there and thank you for your information – I have certainly picked up anything new from right here. I did however expertise some technical issues using this web site, as I experienced to reload the web site a lot of times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for much more of your respective exciting content. Ensure that you update this again soon..

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.