{"id":31703,"date":"2024-11-01T09:01:54","date_gmt":"2024-11-01T09:01:54","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31703"},"modified":"2024-11-01T11:48:36","modified_gmt":"2024-11-01T11:48:36","slug":"python-course-input-and-output-of-python-user-input-and-output","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31703\/","title":{"rendered":"python course: Input and Output of Python, User Input and Output"},"content":{"rendered":"\n<p>Python is a powerful programming language that provides functionalities for handling input and output of data very easily. In this course, we will explore the basic input and output mechanisms provided by Python and how to handle user input in depth. Through this, you will be equipped to create applications that interact directly with data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Standard Input and Output in Python<\/h2>\n\n\n\n<p>To understand input and output in Python, we should start with the most basic functions. The <code>print()<\/code> and <code>input()<\/code> functions are used very frequently in Python programming. The <code>print()<\/code> function is used to display output in the console, while the <code>input()<\/code> function is used to receive user input.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">print() Function<\/h3>\n\n\n\n<p>The <code>print()<\/code> function is used for visualizing data. For example, it can be used to check the value of a variable or to monitor the status of a program.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Output Hello, World!\nprint(\"Hello, World!\")\n<\/code><\/pre>\n\n\n\n<p>In the above code, the string <code>\"Hello, World!\"<\/code> is printed to the console. The <code>print()<\/code> function can output various types of data and can take multiple arguments separated by commas.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 5\nb = \"Python Programming\"\nprint(a, b)\n<\/code><\/pre>\n\n\n\n<p>In the above code, the number 5 and the string &#8220;Python Programming&#8221; are printed, separated by a space. The <code>print()<\/code> function can also flexibly adjust the formatting of the output through additional parameters. For example, it can change the delimiter between printed items or append additional strings at the end.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(a, b, sep=\", \", end=\"!!\\n\")\n<\/code><\/pre>\n\n\n\n<p>In this case, the output will appear as &#8220;5, Python Programming!!&#8221;, where each element is separated by a comma and space, and &#8220;!!&#8221; is added at the end.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">input() Function<\/h3>\n\n\n\n<p>The <code>input()<\/code> function is used to receive user input. This function processes the input as a string, hence, if you want to receive a number, you need to perform type conversion.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Enter your name: \")\nprint(\"Hello,\", name)\n<\/code><\/pre>\n\n\n\n<p>In the above example, when the user inputs their name, it uses that name to print &#8220;Hello, [Name]&#8221;. You should remember that everything the user inputs is processed as a string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Output Formatting<\/h2>\n\n\n\n<p>There are various ways to format output, and to enhance readability and flexibility, Python supports multiple output formatting options.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic String Formatting<\/h3>\n\n\n\n<p>The traditional method of using Python&#8217;s basic string formatting involves using the % symbol within a string to indicate where a variable should appear, and then assigning the variable to that placeholder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = 30\nprint(\"I am %d years old.\" % age)\n<\/code><\/pre>\n\n\n\n<p>This method can also include multiple items using tuples.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Cheolsu\"\nage = 30\nprint(\"%s is %d years old.\" % (name, age))\n<\/code><\/pre>\n\n\n\n<p>However, this method can be somewhat restrictive and less readable compared to modern alternatives.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">str.format() Method<\/h3>\n\n\n\n<p>From Python 2.7 and 3.2 onwards, you can use the <code>str.format()<\/code> method for formatting. This method involves placing {} symbols where you want the variables, and passing the corresponding values as arguments to the <code>format()<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"{} is {} years old.\".format(name, age))\n<\/code><\/pre>\n\n\n\n<p>You can also specify the indexes for clarity on which values should appear in which positions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"{0} is {1} years old.\".format(name, age))\n<\/code><\/pre>\n\n\n\n<p>This method is more readable and provides the advantage of easily modifying the order of variables or output format.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python 3.6 and f-strings<\/h3>\n\n\n\n<p>From Python 3.6 onwards, you can use f-strings. This method involves prefixing the string with f and directly placing variables within curly braces, making it very intuitive and the code more concise.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(f\"{name} is {age} years old.\")\n<\/code><\/pre>\n\n\n\n<p>This method offers great readability and the advantage of directly inserting variables. Additionally, it supports embedded expressions to easily apply complex data transformations or calculations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced User Input Handling<\/h2>\n\n\n\n<p>Handling user input encompasses more than just receiving it; it involves validating the input, providing appropriate feedback, and requesting re-input from the user if necessary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Type Conversion and Exception Handling<\/h3>\n\n\n\n<p>The <code>input()<\/code> function always returns a string, so if you need other data types like numbers, conversion is necessary. Exception handling is required to manage potential errors that may occur during this process.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    age = int(input(\"Please enter your age: \"))\n    print(f\"Your age is {age}.\")\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n<\/code><\/pre>\n\n\n\n<p>In the above code, if the user inputs a non-numeric value, a <code>ValueError<\/code> will be raised, and a friendly error message will be displayed while keeping the program safe.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Repeating Until Valid Input is Received<\/h3>\n\n\n\n<p>You can continuously request valid input from the user until they provide it. This is implemented by requiring input until certain conditions are met.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    try:\n        age = int(input(\"Please enter your age: \"))\n        break  # Stop repeating on valid input\n    except ValueError:\n        print(\"Please enter a valid number.\")\n\nprint(f\"The age entered is {age}.\")\n<\/code><\/pre>\n\n\n\n<p>This iterative structure provides the user with the opportunity to correct invalid input while maintaining the normal flow of the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have learned the basic input and output methods in Python, as well as various techniques for handling user input. Through this, you have likely gained knowledge on how to enhance the flexibility and safety of your programs. In the next tutorial, we will explore more complex data processing techniques such as file input and output.<\/p>\n\n\n\n<p>If you have any questions or need assistance in the meantime, please feel free to ask in the comments section below. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is a powerful programming language that provides functionalities for handling input and output of data very easily. In this course, we will explore the basic input and output mechanisms provided by Python and how to handle user input in depth. Through this, you will be equipped to create applications that interact directly with data. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31703\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python course: Input and Output of Python, User Input and Output&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[98],"tags":[95],"class_list":["post-31703","post","type-post","status-publish","format-standard","hentry","category--en","tag--en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python course: Input and Output of Python, User Input and Output - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31703\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python course: Input and Output of Python, User Input and Output - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Python is a powerful programming language that provides functionalities for handling input and output of data very easily. In this course, we will explore the basic input and output mechanisms provided by Python and how to handle user input in depth. Through this, you will be equipped to create applications that interact directly with data. &hellip; \ub354 \ubcf4\uae30 &quot;python course: Input and Output of Python, User Input and Output&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31703\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:01:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:48:36+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31703\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31703\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python course: Input and Output of Python, User Input and Output\",\"datePublished\":\"2024-11-01T09:01:54+00:00\",\"dateModified\":\"2024-11-01T11:48:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31703\/\"},\"wordCount\":746,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"keywords\":[\"\ud30c\uc774\uc36c\uac15\uc88c\"],\"articleSection\":[\"Python Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31703\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31703\/\",\"name\":\"python course: Input and Output of Python, User Input and Output - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:01:54+00:00\",\"dateModified\":\"2024-11-01T11:48:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31703\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31703\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31703\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python course: Input and Output of Python, User Input and Output\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"python course: Input and Output of Python, User Input and Output - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/31703\/","og_locale":"ko_KR","og_type":"article","og_title":"python course: Input and Output of Python, User Input and Output - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Python is a powerful programming language that provides functionalities for handling input and output of data very easily. In this course, we will explore the basic input and output mechanisms provided by Python and how to handle user input in depth. Through this, you will be equipped to create applications that interact directly with data. &hellip; \ub354 \ubcf4\uae30 \"python course: Input and Output of Python, User Input and Output\"","og_url":"https:\/\/atmokpo.com\/w\/31703\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:01:54+00:00","article_modified_time":"2024-11-01T11:48:36+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31703\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31703\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python course: Input and Output of Python, User Input and Output","datePublished":"2024-11-01T09:01:54+00:00","dateModified":"2024-11-01T11:48:36+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31703\/"},"wordCount":746,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"keywords":["\ud30c\uc774\uc36c\uac15\uc88c"],"articleSection":["Python Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31703\/","url":"https:\/\/atmokpo.com\/w\/31703\/","name":"python course: Input and Output of Python, User Input and Output - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:01:54+00:00","dateModified":"2024-11-01T11:48:36+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31703\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31703\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31703\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"python course: Input and Output of Python, User Input and Output"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31703"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31703\/revisions"}],"predecessor-version":[{"id":31704,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31703\/revisions\/31704"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}