Prev: Print      |      Next: -

# Input¶

## input()¶

• The input() function is used for reading users' input from keyboard
• Usually, we also pass to input a string argument to be displayed as a prompt to the user
In [ ]:
name = input('Please enter your name: ')

print('{:10}{:5d}{:6.2f}'.format(name, age, h))


#### input() returns 'string' type¶

• In Python 3 input returns a string.
• So, we need to change the input type accordingly, if we plan to manipulate the input as numerical. This is typically accomplished by using the int() and float() methods.
In [ ]:
part = 1000
pers = int(input('Please enter personal expenses: '))
print('Total expenses are: ', part + pers)


#### How to read two (or more) values from keyboard in a single line¶

• (a) Use two input() functions as in the example below (use as many input() functions as names on the left part of the assignment)
In [6]:
scoreA, scoreB = input('Enter score for player A:'), input('Enter score for player B:')
scoreA, scoreB

Enter score for player A:5
Enter score for player B:7

Out[6]:
('5', '7')
• (b) Use the split() method as you see below
• Split() separates the string input we give using as delimiter the character that we give as argument to split() (if none then space is used)
In [8]:
scoreA, scoreB = input('Enter the score of each player (separate with space):').split()
scoreA, scoreB

Enter the score of each player (separate with space):12 15

Out[8]:
('12', '15')

. Free learning material
. See full copyright and disclaimer notice