User Input

Basic Syntax

Python allows for user input by usin built-in function using input() which will always returns a str value:

prompt_name = input(<prompt_string>)

Therefore, there may be instances where a Type Conversion is required to convert a string value into a float or int to b used as numerical values.

How does this work?

Python will pause the program and wait for the input from the user before proceeding. This function allows the programmer to provide a prompt string to ask the user for an input.


Taking Multiple User input

Python user can take multiple values or inputs in one line using one of two methods:

1. Using split() Method

We can use the str.split() method to break the given input by the specified separator. By default, it will split based on any whitespace.

inp = input('Prompt String').split()

2. List Comprehension

We can also use a List Comprehension to retrieve multiple inputs in one line of code:

# Taking multiple values
x = [int(x) for x in input("Enter multiple values (separate by comma): ").split(',')]
Powered by Forestry.md