×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Taking multiple inputs from the user using split() method in Python

By IncludeHelp Last updated : December 08, 2024

Prerequisites

To understand the given examples for taking multiple user inputs, you should have knowledge of the following Python topics:

Taking multiple inputs at once

To take multiple inputs from the user in Python, you can simply use the combination of input() and split() method like input().split() and write the multiple values as input with separators/delimiters.

Let's understand the split() method.

split() method

The split() method is one of the library methods in Python, it accepts multiple inputs and brakes the given input based on the provided separator if we don't provide any separator any whitespace is considered as the separator.

Syntax to take multiple user inputs in Python

Below is the syntax to take multiple user inputs with separators:

input("Prompt Message").split([separator], [maxsplit])

Here, separator and maxsplit are the optional parameters, they can be used for the specified purposes.

Taking multiple inputs separated by whitespace

If the given input is separated by the whitespaces, you can use the .split() method without specifying the separator/delimiter.

Example

# input two integer numbers and print them

# Taking multiple inputs
a, b = input("Enter two integer numbers : ").split()

# Printing the values
print("a :", a, "b :", b)

# input name, age and percentages of the student
# and print them
name, age, perc = input("Enter student's details: ").split()

print("Name :", name)
print("Age :", age)
print("Percentage :", perc)

Output

Enter two integer numbers : 100 200
a : 100 b : 200
Enter student's details: Bharat 21 78
Name : Bharat
Age : 21
Percentage : 78

Taking multiple inputs separated by commas

If the given input is separated by the commas, you can use .split() method by specifying the comma (,) as a separator/delimiter in it.

Example

# input two integer numbers and print them

# Taking multiple inputs
a, b = input("Enter two integer numbers : ").split(",")

# Printing the values
print("a :", a, "b :", b)

# input name, age and percentages of the student
# and print them
name, age, perc = input("Enter student's details: ").split(",")

print("Name :", name)
print("Age :", age)
print("Percentage :", perc)

Output

Enter two integer numbers : 100,200
a : 100 b : 200
Enter student's details: Bharat,21,99
Name : Bharat
Age : 21
Percentage : 99

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.