{"id":6590,"date":"2023-01-11T08:59:20","date_gmt":"2023-01-11T08:59:20","guid":{"rendered":"https:\/\/tutorpython.com\/?p=6590"},"modified":"2023-10-08T19:03:12","modified_gmt":"2023-10-08T13:33:12","slug":"basic-input-output-operations-in-python","status":"publish","type":"post","link":"https:\/\/tutorpython.com\/basic-input-output-operations-in-python","title":{"rendered":"Basic Input Output Operations in Python"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">In This Article<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Input_operations_in_Python\" >Input operations in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#String_input_in_Python\" >String input in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Input_operation_using_Type_Conversion\" >Input operation using Type Conversion<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Output_operations_in_Python\" >Output operations in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Basic_Print_statement_in_Python\" >Basic Print statement in Python<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Output_in_a_single_line\" >Output in a single line<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Print_statement_using_end_parameter_in_Python\" >Print statement using end parameter in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Print_statement_using_separator_parameter_in_Python\" >Print statement using separator parameter in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Print_statement_using_Format_String_for_Output_Operations_in_Python\" >Print statement using Format String for Output Operations in Python<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/tutorpython.com\/basic-input-output-operations-in-python\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p>In this article, we will learn about the basic <a href=\"https:\/\/tutorpython.com\/tutorial\/basic-input-output-operations-in-python\">input output operations in Python<\/a>. There are different ways of using the input and output properties of the Python programming language which we are going to discuss here.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Input_operations_in_Python\"><\/span>Input operations in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As programmers when we are writing our code, we sometimes require to provide some input to the computer. This can be done by using the <strong>input()<\/strong> <a href=\"https:\/\/tutorpython.com\/tutorial\/functions-in-python\" target=\"_blank\" rel=\"noopener\">function in Python<\/a>.<\/p>\n<p><strong>Note:<\/strong> The input() function takes the input from the user and by default returns it as a <strong>string<\/strong> data type. We need to use a different <a href=\"https:\/\/tutorpython.com\/tutorial\/type-conversions-in-python\" target=\"_blank\" rel=\"noopener\">type of conversion<\/a> technique if we want to change the data type of our inputs.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"String_input_in_Python\"><\/span>String input in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Example of how to take input as a <a href=\"https:\/\/tutorpython.com\/tutorial\/strings-in-python\" target=\"_blank\" rel=\"noopener\">String in Python<\/a><\/p>\n<pre><code class=\"language-python\">name = input(\"Enter your name: \")\r\nprint(\"Hello, \" + name)<\/code><\/pre>\n<p>Here, we ask the user to enter their name and then print a greeting using their name.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Input_operation_using_Type_Conversion\"><\/span>Input operation using Type Conversion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To read an integer from the user, we can use the <strong>int()<\/strong> function.<\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-python\">age1 = int(input(\"Enter age of father: \"))\r\nprint(age1)\r\nage2 = int(input(\"Enter age of son: \"))\r\nprint(age2)\r\nage_sum = age1 + age2\r\nprint(\"Combined age of father and son is \" + str(age_sum) + \" years.\")<\/code><\/pre>\n<p>The output of this will be:<br \/>\n<code>50<br \/>\n20<br \/>\nCombined age of father and son is 70 years.<\/code><\/p>\n<p>Here, we ask the user to enter the age of the father and son, and then print it out.<\/p>\n<p>We use the <strong>int()<\/strong> function to <a href=\"https:\/\/tutorpython.com\/tutorial\/numeric-data-type-conversions-in-python\" target=\"_blank\" rel=\"noopener\">change the data type<\/a> of their age variables so we can calculate their sum. Then we need to convert the age to a string using the <strong>str()<\/strong> function before we can <strong>concatenate<\/strong> it with the output strings.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Output_operations_in_Python\"><\/span>Output operations in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>While writing our code we need to show the output of certain actions implemented inside our code. We can do that using the <strong>print()<\/strong> function. The full <a href=\"https:\/\/tutorpython.com\/tutorial\/python-syntax\" target=\"_blank\" rel=\"noopener\">syntax<\/a> of the print() function is:<\/p>\n<p><code>print(object= separator= end= file= flush=)<\/code><\/p>\n<p>where,<\/p>\n<ol>\n<li><strong>object<\/strong> &#8211; values that will be printed. This is a compulsory field.<\/li>\n<li><strong>separator<\/strong> &#8211; allows us to separate multiple objects inside print().<\/li>\n<li><strong>end<\/strong> &#8211; allows us to add specific values like new line &#8220;\\n&#8221;, tab &#8220;\\t&#8221;.<\/li>\n<li><strong>file<\/strong> &#8211; where the values are printed.<\/li>\n<li><strong>flush<\/strong> &#8211; boolean specifying if the output is flushed or buffered. The default value is False.<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"Basic_Print_statement_in_Python\"><\/span>Basic Print statement in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Example of how to print a value in Python<\/p>\n<pre><code class=\"language-python\">print(\"Hello World!\")<\/code><\/pre>\n<p>The output of this will be:<br \/>\n<code>Hello World!<\/code><\/p>\n<p>Here, the print() function only uses the object parameter.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Output_in_a_single_line\"><\/span>Output in a single line<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Print_statement_using_end_parameter_in_Python\"><\/span>Print statement using <strong>end<\/strong> parameter in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Example:<\/p>\n<pre><code class=\"language-python\">print(\"Hello World\", end=', ')\r\nprint(\"this is 2023.\")<\/code><\/pre>\n<p>The output of this will be:<br \/>\n<code>Hello World, this is 2023.<\/code><\/p>\n<p>Here, we use the <strong>end<\/strong> parameter to print out two separate strings by ending the first string with a comma so that we get the output in a single line.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Print_statement_using_separator_parameter_in_Python\"><\/span>Print statement using <b>separator<\/b> parameter in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Example:<\/p>\n<pre><code class=\"language-python\">print(\"Hello World\", \"this is\", \"2023\", sep='...')<\/code><\/pre>\n<p>The output to this will be:<br \/>\n<code>Hello World...this is...2023<\/code><\/p>\n<p>Here, we use the <strong>sep<\/strong> parameter to print out more than one object inside a print statement together in one line.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Print_statement_using_Format_String_for_Output_Operations_in_Python\"><\/span>Print statement using <strong>Format<\/strong> String for Output Operations in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Example:<\/p>\n<pre><code class=\"language-python\">x = 5\r\ny = 10\r\nprint(f'The value of x is {x} and y is {y}.')<\/code><\/pre>\n<p>The output of this will be:<br \/>\n<code>The value of x is 5 and y is 10.<\/code><\/p>\n<p>Here, we use the format string to directly insert the values inside the string during the print() operation. This is done using the curly braces( <strong>{ }<\/strong> ) as placeholders. We can insert multiple values using this method.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we learned about the basic input and output operations in Python. We saw examples of how to use the <code>input()<\/code> function to read input from the user and the <code>print()<\/code> function to print output on the screen. We also saw how to use string formatting to perform more advanced output operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn about the basic input output operations&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-6590","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"acf":[],"_links":{"self":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/6590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/comments?post=6590"}],"version-history":[{"count":7,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/6590\/revisions"}],"predecessor-version":[{"id":8792,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/6590\/revisions\/8792"}],"wp:attachment":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/media?parent=6590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/categories?post=6590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/tags?post=6590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}