{"id":23836,"date":"2022-08-19T15:32:12","date_gmt":"2022-08-19T10:02:12","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=23836"},"modified":"2026-07-13T12:40:47","modified_gmt":"2026-07-13T07:10:47","slug":"python-read-input-from-stdin","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/","title":{"rendered":"Read stdin in Python: input(), sys.stdin, Pipes, and EOF"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> Use <code>input()<\/code> for one interactive line and <code>sys.stdin<\/code> when a program must consume piped or redirected text. Decide whether to strip newlines, handle EOF, and process the stream lazily before choosing how much input to read.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-stdin-input-stream.png\" alt=\"Python stdin diagram comparing input prompts, sys.stdin streams, pipes, EOF handling, bytes, stderr, and StringIO tests\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>input() is interactive, sys.stdin is a stream, and EOF, output routing, and text-versus-bytes boundaries are part of the CLI contract.<\/figcaption><\/figure>\n<p><code>stdin<\/code> is the standard input stream for a Python process. It is where text arrives when a user types into a terminal, pipes data into a command, or redirects a file into a script. Python gives you several ways to read it, depending on whether you need one prompt, one line, or a stream of many lines.<\/p>\n<p>Use <code>input()<\/code> for interactive prompts. Use <code>sys.stdin<\/code> when input may come from a pipe or a redirected file. Use <code>fileinput<\/code> when a script should read either named files or standard input with the same loop.<\/p>\n<p>The right choice depends on how the program is called. A teaching script may ask one question and stop. A command-line utility may receive hundreds of lines from another command. A test may replace the stream with a small in-memory object. Treat those cases separately instead of forcing every script through one pattern.<\/p>\n<p>The official <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#input\">input documentation<\/a> covers interactive reads, the <a href=\"https:\/\/docs.python.org\/3\/library\/sys.html#sys.stdin\">sys.stdin documentation<\/a> describes the standard stream, the <a href=\"https:\/\/docs.python.org\/3\/library\/fileinput.html\">fileinput documentation<\/a> covers file-or-stdin loops, and the <a href=\"https:\/\/docs.python.org\/3\/library\/unittest.mock.html\">unittest.mock documentation<\/a> helps with tests.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Read_One_Interactive_Value\" >Read One Interactive Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Read_One_Line_With_sysstdin\" >Read One Line With sys.stdin<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Read_All_Input_At_Once\" >Read All Input At Once<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Iterate_Over_stdin\" >Iterate Over stdin<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Use_fileinput_For_Files_Or_stdin\" >Use fileinput For Files Or stdin<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Test_Code_That_Reads_stdin\" >Test Code That Reads stdin<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Practical_Guidance\" >Practical Guidance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Handle_EOF_explicitly\" >Handle EOF explicitly<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Read_all_input_only_when_it_fits\" >Read all input only when it fits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Separate_text_and_binary_streams\" >Separate text and binary streams<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Test_stdin_without_hanging\" >Test stdin without hanging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#How_do_I_read_one_line_from_stdin_in_Python\" >How do I read one line from stdin in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#How_do_I_read_all_stdin_in_Python\" >How do I read all stdin in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#How_do_I_handle_end-of-file_when_using_input\" >How do I handle end-of-file when using input()?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#How_can_I_test_code_that_reads_stdin\" >How can I test code that reads stdin?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Read_One_Interactive_Value\"><\/span>Read One Interactive Value<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>input()<\/code> reads one line from standard input, removes the trailing newline, and returns a string.<\/p>\n<pre><code class=\"language-python\">name = input(\"Name: \")\nmessage = f\"Hello, {name}\"\n\nprint(message)\n<\/code><\/pre>\n<p>This is best for interactive scripts where a person is expected to type a response. It is less convenient for large piped input because it reads one line at a time.<\/p>\n<p>Use prompts only when a human is present. If the script will run in automation, prefer reading from <code>sys.stdin<\/code> or accepting file paths so the caller can provide input without manual typing.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Read_One_Line_With_sysstdin\"><\/span>Read One Line With sys.stdin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>sys.stdin.readline()<\/code> when you want direct access to the stream.<\/p>\n<pre><code class=\"language-python\">import sys\n\nline = sys.stdin.readline()\nclean_line = line.rstrip(\"\\n\")\n\nprint(clean_line.upper())\n<\/code><\/pre>\n<p><code>readline()<\/code> keeps the newline unless you remove it. Use <code>rstrip(\"\\n\")<\/code> when only the final newline should be removed.<\/p>\n<p>Be careful with <code>strip()<\/code> when leading or trailing spaces matter. For structured text, removing only the newline is often safer than removing all surrounding whitespace.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/stdin-input-b206.png\" alt=\"Python Pool infographic showing a terminal, prompt, input, and returned string\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>input() reads one interactive line and returns text after the user submits it.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Read_All_Input_At_Once\"><\/span>Read All Input At Once<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For small inputs, <code>sys.stdin.read()<\/code> loads the entire stream into memory.<\/p>\n<pre><code class=\"language-python\">import sys\n\ntext = sys.stdin.read()\nlines = text.splitlines()\n\nprint(\"line count:\", len(lines))\nprint(\"character count:\", len(text))\n<\/code><\/pre>\n<p>This is simple for short redirected files or test data. Avoid it for very large streams because it waits for all input and stores the full content.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Iterate_Over_stdin\"><\/span>Iterate Over stdin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For streaming data, iterate over <code>sys.stdin<\/code>. This processes one line at a time.<\/p>\n<pre><code class=\"language-python\">import sys\n\nfor line_number, line in enumerate(sys.stdin, start=1):\n    text = line.strip()\n    if text:\n        print(line_number, text)\n<\/code><\/pre>\n<p>This pattern is good for logs, lists, and command pipelines. It starts producing output as lines arrive and does not need the whole stream in memory.<\/p>\n<p>Line-by-line processing is also easier to recover from. If one line is invalid, the script can report the problem to <code>stderr<\/code> and continue with the next line when that behavior makes sense.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/stdin-sys-stdin-b206.png\" alt=\"Python Pool infographic mapping a stdin stream through iteration to incoming lines\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>sys.stdin supports stream-oriented processing from pipes, files, or interactive input.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_fileinput_For_Files_Or_stdin\"><\/span>Use fileinput For Files Or stdin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>fileinput<\/code> module can read file names supplied to a script, or standard input when no file names are supplied.<\/p>\n<pre><code class=\"language-python\">import fileinput\n\nfor line in fileinput.input():\n    clean = line.strip()\n    if clean:\n        print(clean.lower())\n<\/code><\/pre>\n<p>This is useful for Unix-style tools where users expect the same program to accept piped input or file paths.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Test_Code_That_Reads_stdin\"><\/span>Test Code That Reads stdin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Patch <code>sys.stdin<\/code> with <code>io.StringIO<\/code> when testing code that reads from standard input.<\/p>\n<pre><code class=\"language-python\">import io\nfrom unittest.mock import patch\n\ndef read_first_line():\n    return input().strip()\n\nwith patch(\"sys.stdin\", io.StringIO(\"alpha\\n\")):\n    result = read_first_line()\n\nprint(result)\n<\/code><\/pre>\n<p>This makes tests repeatable without waiting for real terminal input. It also lets you cover empty input, multiple lines, and whitespace cases.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Practical_Guidance\"><\/span>Practical Guidance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Choose the smallest API that matches the job. Use <code>input()<\/code> for one human prompt, <code>readline()<\/code> for one stream line, <code>read()<\/code> for a small full payload, and iteration for large or ongoing streams.<\/p>\n<p>Keep normal output on <code>stdout<\/code> and diagnostics on <code>stderr<\/code>. That way a script can read from <code>stdin<\/code>, produce clean output, and still show warnings without breaking another program that consumes the result.<\/p>\n<p>Always decide how empty input should behave. Some scripts should print nothing, some should show a helpful error, and some should use defaults. Make that choice explicit so command-line behavior is predictable.<\/p>\n<p>If input may be binary, use <code>sys.stdin.buffer<\/code> instead of text APIs. For ordinary command-line text, the examples above are the usual starting point.<\/p>\n<p>For reliable tools, document what the program expects from <code>stdin<\/code>: one line, many lines, JSON text, CSV rows, or raw bytes. Clear input rules make the script easier to use in terminals, CI jobs, and other Python programs.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/stdin-pipe-b206.png\" alt=\"Python Pool infographic showing a producer process, pipe, stdin, and consumer program\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Pipes connect a producer&#039;s output to a program&#039;s standard input.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Handle_EOF_explicitly\"><\/span>Handle EOF explicitly<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Interactive users may send EOF, and a pipe may end normally without producing the number of records you expected. Catch <code>EOFError<\/code> around one <code>input()<\/code> call, or let iteration over <code>sys.stdin<\/code> finish naturally and validate the record count afterward.<\/p>\n<pre><code class=\"language-python\">try:\n    name = input(\"Name: \")\nexcept EOFError:\n    name = \"anonymous\"\n\nprint(name)\n<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Read_all_input_only_when_it_fits\"><\/span>Read all input only when it fits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>sys.stdin.read()<\/code> is convenient for a small document, but it waits for EOF and stores the complete input in memory. For large streams, iterate line by line and process each record as it arrives. Use <code>readline()<\/code> when the protocol has a clear one-line boundary.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/stdin-check-b206.png\" alt=\"Python Pool infographic testing EOF, buffering, encoding, prompts, and validation\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Check EOF, interactive versus piped input, encoding, buffering, and prompt behavior.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Separate_text_and_binary_streams\"><\/span>Separate text and binary streams<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>sys.stdin<\/code> is a text stream with decoding and newline behavior. If a tool must consume raw bytes, use <code>sys.stdin.buffer<\/code> when available. Keep decoding policy at the boundary, and remember that test doubles may not provide a <code>buffer<\/code> attribute.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Test_stdin_without_hanging\"><\/span>Test stdin without hanging<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Inject a string stream in tests instead of waiting for a real terminal. Save and restore <code>sys.stdin<\/code> with a fixture, and test empty input, one line, multiple lines, and EOF. Keep normal output on stdout and diagnostics on stderr.<\/p>\n<p data-pythonpool-link-set=\"2026-07-13-final\">For command-line input, compare reading stdin with Python -m flags and argparse booleans. Read <a href=\"https:\/\/www.pythonpool.com\/python-m-flag\/\">python m flag<\/a> and <a href=\"https:\/\/www.pythonpool.com\/python-argparse-boolean\/\">python argparse boolean<\/a> for the related workflow.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_read_one_line_from_stdin_in_Python\"><\/span>How do I read one line from stdin in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use input() for an interactive prompt or sys.stdin.readline() when you need direct control over the standard input stream.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_read_all_stdin_in_Python\"><\/span>How do I read all stdin in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Call sys.stdin.read() for small inputs when loading the entire stream is acceptable, or iterate over sys.stdin for streaming data.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_handle_end-of-file_when_using_input\"><\/span>How do I handle end-of-file when using input()?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Catch EOFError for interactive input, or prefer a stream loop when the command is expected to receive piped or redirected data.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_can_I_test_code_that_reads_stdin\"><\/span>How can I test code that reads stdin?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Replace sys.stdin with io.StringIO or another file-like object in the test so the code can consume deterministic input without a real terminal.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"How do I read one line from stdin in Python?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use input() for an interactive prompt or sys.stdin.readline() when you need direct control over the standard input stream.\"}},{\"@type\":\"Question\",\"name\":\"How do I read all stdin in Python?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Call sys.stdin.read() for small inputs when loading the entire stream is acceptable, or iterate over sys.stdin for streaming data.\"}},{\"@type\":\"Question\",\"name\":\"How do I handle end-of-file when using input()?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Catch EOFError for interactive input, or prefer a stream loop when the command is expected to receive piped or redirected data.\"}},{\"@type\":\"Question\",\"name\":\"How can I test code that reads stdin?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Replace sys.stdin with io.StringIO or another file-like object in the test so the code can consume deterministic input without a real terminal.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Read Python standard input interactively or as a stream, with explicit choices for prompts, pipes, EOF, text, bytes, and tests.<\/p>\n","protected":false},"author":32,"featured_media":33654,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[5394,5392,5393,5399,5398,5397],"class_list":["post-23836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-python-read-binary-data-from-stdin","tag-python-read-input-from-stdin-line-by-line","tag-python-read-input-from-stdin-password","tag-python-read-number-from-stdin","tag-python-read-stdin-until-character","tag-read-input-file-from-stdin-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Read stdin in Python: input(), sys.stdin, Pipes, and EOF<\/title>\n<meta name=\"description\" content=\"Read Python stdin interactively or from pipes with input(), sys.stdin, iteration, EOF handling, binary boundaries, and tests.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Read stdin in Python: input(), sys.stdin, Pipes, and EOF\" \/>\n<meta property=\"og:description\" content=\"Read Python standard input interactively or as a stream, with explicit choices for prompts, pipes, EOF, text, bytes, and tests.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-19T10:02:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:10:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-stdin-input-stream.png\" \/>\n<meta name=\"author\" content=\"Yugmil Patel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-stdin-input-stream.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yugmil Patel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/\"},\"author\":{\"name\":\"Yugmil Patel\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/93f03f9a968d1187cbce46a3586cdac5\"},\"headline\":\"Read stdin in Python: input(), sys.stdin, Pipes, and EOF\",\"datePublished\":\"2022-08-19T10:02:12+00:00\",\"dateModified\":\"2026-07-13T07:10:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/\"},\"wordCount\":1081,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-read-stdin-guide-pythonpool.png\",\"keywords\":[\"python read binary data from stdin\",\"python read input from stdin line by line\",\"python read input from stdin password\",\"python read number from stdin\",\"python read stdin until character\",\"read input file from stdin python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/\",\"name\":\"Read stdin in Python: input(), sys.stdin, Pipes, and EOF\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-read-stdin-guide-pythonpool.png\",\"datePublished\":\"2022-08-19T10:02:12+00:00\",\"dateModified\":\"2026-07-13T07:10:47+00:00\",\"description\":\"Read Python stdin interactively or from pipes with input(), sys.stdin, iteration, EOF handling, binary boundaries, and tests.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-read-stdin-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-read-stdin-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Python read stdin guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-read-input-from-stdin\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Read stdin in Python: input(), sys.stdin, Pipes, and EOF\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/93f03f9a968d1187cbce46a3586cdac5\",\"name\":\"Yugmil Patel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2d861c3b6baf176f665bbbb1201d50ca0c6a8a7ee922b55157086205707c341d?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2d861c3b6baf176f665bbbb1201d50ca0c6a8a7ee922b55157086205707c341d?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2d861c3b6baf176f665bbbb1201d50ca0c6a8a7ee922b55157086205707c341d?s=96&d=wavatar&r=g\",\"caption\":\"Yugmil Patel\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Read stdin in Python: input(), sys.stdin, Pipes, and EOF","description":"Read Python stdin interactively or from pipes with input(), sys.stdin, iteration, EOF handling, binary boundaries, and tests.","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:\/\/www.pythonpool.com\/python-read-input-from-stdin\/","og_locale":"en_US","og_type":"article","og_title":"Read stdin in Python: input(), sys.stdin, Pipes, and EOF","og_description":"Read Python standard input interactively or as a stream, with explicit choices for prompts, pipes, EOF, text, bytes, and tests.","og_url":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/","og_site_name":"Python Pool","article_published_time":"2022-08-19T10:02:12+00:00","article_modified_time":"2026-07-13T07:10:47+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-stdin-input-stream.png","type":"","width":"","height":""}],"author":"Yugmil Patel","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-stdin-input-stream.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Yugmil Patel","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/"},"author":{"name":"Yugmil Patel","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/93f03f9a968d1187cbce46a3586cdac5"},"headline":"Read stdin in Python: input(), sys.stdin, Pipes, and EOF","datePublished":"2022-08-19T10:02:12+00:00","dateModified":"2026-07-13T07:10:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/"},"wordCount":1081,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-read-stdin-guide-pythonpool.png","keywords":["python read binary data from stdin","python read input from stdin line by line","python read input from stdin password","python read number from stdin","python read stdin until character","read input file from stdin python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/","url":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/","name":"Read stdin in Python: input(), sys.stdin, Pipes, and EOF","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-read-stdin-guide-pythonpool.png","datePublished":"2022-08-19T10:02:12+00:00","dateModified":"2026-07-13T07:10:47+00:00","description":"Read Python stdin interactively or from pipes with input(), sys.stdin, iteration, EOF handling, binary boundaries, and tests.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-read-stdin-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-read-stdin-guide-pythonpool.png","width":1350,"height":650,"caption":"Python read stdin guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-read-input-from-stdin\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Read stdin in Python: input(), sys.stdin, Pipes, and EOF"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/93f03f9a968d1187cbce46a3586cdac5","name":"Yugmil Patel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2d861c3b6baf176f665bbbb1201d50ca0c6a8a7ee922b55157086205707c341d?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2d861c3b6baf176f665bbbb1201d50ca0c6a8a7ee922b55157086205707c341d?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2d861c3b6baf176f665bbbb1201d50ca0c6a8a7ee922b55157086205707c341d?s=96&d=wavatar&r=g","caption":"Yugmil Patel"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/23836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=23836"}],"version-history":[{"count":18,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/23836\/revisions"}],"predecessor-version":[{"id":41904,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/23836\/revisions\/41904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33654"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=23836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=23836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=23836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}