Sunday, April 10, 2016

Obtaining user input in Python



Obtaining user input:
# Initialize a variable with a user-specified value.
user = input( ‘I am Python. What is your name? : ‘ )
# Output a string and a variable value.
print( ‘Welcome’ , user )

When multiple values are specified to the print() function it will display each value in output separated by a single space by default.
Output displayed by the print() function.
An alternative separator can, however, be specified by adding a sep parameter to the comma-separated list.

For example sep = ’*’ will display each value in output separated by an asterisk character.

By default print() function add an invisible \n newline
character at the end of the line to automatically move the print head to the next line.
An alternative line ending can, however, be specified by adding an end parameter to the comma-separated list.

For example, end = ’!’ will display each value in output then end the
line with an exclamation mark.

# Initialize another variable with a user-specified value.
lang = input( ‘Favorite programming language? : ‘ )
# Output a string and a variable value.
print( lang , ‘Is’ , ‘Fun’ , sep = ‘ * ‘ , end = ‘!\n’ )