{"id":869,"date":"2022-10-12T16:05:00","date_gmt":"2022-10-12T10:35:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=869"},"modified":"2023-08-15T16:20:16","modified_gmt":"2023-08-15T10:50:16","slug":"multiple-inputs-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/multiple-inputs-in-python","title":{"rendered":"Take Multiple Inputs From The User In A Single Line Of Python Code"},"content":{"rendered":"\n<p>In Python, the&nbsp;<strong><em>input()<\/em><\/strong>&nbsp;function allows taking input from the user, and to provide a message with the&nbsp;<code>input()<\/code>&nbsp;function, we can prompt a string with it.<\/p>\n\n\n\n<p>If we use a simple approach like a beginner then we would write a Python program like below to take multiple inputs.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">x = input(\"Enter the input here: \")\ny = input(\"Enter the input here: \")\nz = input(\"Enter the input here: \")<\/pre><\/div>\n\n\n\n<p>There are some ways through which we can restrict the above code to a single line for taking multiple inputs from the user in Python. One-liners in Python are always powerful which helps us to write complex code in one line which in turn increases productivity and saves time.<\/p>\n\n\n\n<p>We are going to see two methods through which we can achieve our goal<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>split() method<\/strong><\/li>\n\n\n\n<li><strong>list comprehension<\/strong><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-split-method\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-using-split-method\"><\/a>Using split() method<\/h1>\n\n\n\n<p>Generally, the&nbsp;<code>split()<\/code>&nbsp;method is used to split a Python string into a list but we can use it for taking multiple inputs from the user.<\/p>\n\n\n\n<p>It will split the specified values in the&nbsp;<strong>input()<\/strong>&nbsp;function using a specified separator and if there is no specified separator then any whitespace is a separator.<\/p>\n\n\n\n<p>Here we\u2019ll use&nbsp;<strong>split()<\/strong>&nbsp;method with&nbsp;<strong>input()<\/strong>&nbsp;function.<\/p>\n\n\n\n<p><code>input().split(separator, maxsplit)<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>separator<\/code>: The string will separate at the specified separator. If it\u2019s not provided then any whitespace is a separator.<\/li>\n\n\n\n<li><code>maxsplit<\/code>: A string will split into a maximum of a specified number of times. The default is -1, which means there is no limit.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-taking-multiple-inputs-at-a-time\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-taking-multiple-inputs-at-a-time\"><\/a>Taking multiple inputs at a time<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">x, y, z = input(\"Enter 3 inputs: \").split()\nprint(f\"\\n1st input: {x}\")\nprint(f\"2nd input: {y}\")\nprint(f\"3rd input: {z}\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Enter 3 inputs: Hello there geeks\n1st input: Hello\n2nd input: there\n3rd input: geeks<\/pre><\/div>\n\n\n\n<p>We haven\u2019t defined any separator and maxsplit parameters. Here we can use only three inputs because we\u2019ve defined only three variables.<\/p>\n\n\n\n<p>But there is a way to take unlimited inputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-taking-unlimited-inputs\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-taking-unlimited-inputs\"><\/a>Taking unlimited inputs<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">x = input(\"Enter your inputs: \").split()\nprint(f\"\\nInputs: {x}\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Enter your inputs: Hello there geeks I am Sachin\nInputs: ['Hello', 'there', 'geeks', 'I', 'am', 'Sachin']<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-taking-inputs-using-separator-and-maxsplit\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-taking-inputs-using-separator-and-maxsplit\"><\/a>Taking inputs using separator and maxsplit<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Using a comma as a separator\nx, y, z = input(\"Enter the inputs with commas: \").split(\",\")\nprint(f\"\\n1st input: {x}\")\nprint(f\"2nd input: {y}\")\nprint(f\"3rd input: {z}\")\n\n# Using maxsplit\nx = input(\"Enter your input: \").split(\",\", maxsplit=1)\nprint(f\"\\nMaxsplit 1: {x}\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Enter the inputs with commas: hey, there, geeks\n1st input: hey\n2nd input:  there\n3rd input:  geeks\n\nEnter your input: hey, there, geeks\nMaxsplit 1: ['hey', 'there, geeks']<\/pre><\/div>\n\n\n\n<p>In the first block of code, we specified the separator (<code>,<\/code>) and the inputs were separated by the commas hence we got the three separate outputs.<\/p>\n\n\n\n<p>In the second block of code, we specified a separator and&nbsp;<strong>maxsplit=1<\/strong>&nbsp;and we can see the output is split into two parts instead of getting three separate outputs.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-list-comprehension\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-using-list-comprehension\"><\/a>Using list comprehension<\/h1>\n\n\n\n<p>It is the same process as we did in the first half of this article. Basically, we are going to use the split() but in the&nbsp;<strong>list comprehension<\/strong>&nbsp;method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-taking-multiple-inputs-at-a-time\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-taking-multiple-inputs-at-a-time\"><\/a>Taking multiple inputs at a time<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">x = [x for x in input(\"Enter your inputs: \").split()]\n\nprint(\"Inputs: \", x)\n\n# Taking integers as an input\ny = [int(y) for y in input(\"Enter the values: \").split()]\n\nprint(\"List of students: \", y)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Enter your inputs: There are 4 students in a class of 30 students\nInputs:  ['There', 'are', '4', 'students', 'in', 'a', 'class', 'of', '30', 'students']\nEnter the values: 23 43 30 45\nList of students:  [23, 43, 30, 45]<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-using-a-separator\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-using-a-separator\"><\/a>Using a separator<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Using separator\ny = [int(y) for y in input(\"Enter the values: \").split(\"-\")]\n\nprint(\"List of students: \", y)\n\n# Using letter as a separator\ny = [y for y in input(\"Enter the values: \").split(\"s\")]\n\nprint(\"List of animals: \", y)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Enter the values: 30-45-67-34-23-41\nList of students:  [30, 45, 67, 34, 23, 41]\nEnter the values: DogsCatsRacoonsSpider\nList of animals:  ['Dog', 'Cat', 'Racoon', 'Spider']<\/pre><\/div>\n\n\n\n<p>We can use any type of separator whether it can be punctuation or letters.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/multiple-inputs-in-python#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>One-liners can be very useful and helpful to enhance code quality as well as increase productivity. Well, we learned how to take multiple inputs from users in a single line of Python code instead of writing multiple lines of code.<\/p>\n\n\n\n<p>We used the split() method which is primarily used for splitting the Python string into a list and then we used the list comprehension technique which offers a shorter syntax to define and create a list in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That\u2019s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, the&nbsp;input()&nbsp;function allows taking input from the user, and to provide a message with the&nbsp;input()&nbsp;function, we can prompt a string with it. If we use a simple approach like a beginner then we would write a Python program like below to take multiple inputs. There are some ways through which we can restrict the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":871,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,7],"tags":[12,53,31],"class_list":["post-869","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-python-tips","tag-python","tag-python-tips","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=869"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":1355,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/869\/revisions\/1355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/871"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}