Input and Output in Python

How to take the input from the user?

You must know that taking Input and Output from the user is one of the essential parts of any Python Program. In Python we can use input() function to take inputs from the ourtside world. The syntax for the input() function is:

input([prompt])

Here prompt (inside square brackets means it is optional) can be any string (message) we wish to display while asking user for input. Though prompt is optional, but it should always be provided, since it is important to tell the user – What you are expecting them to enter (Age, Name, Address or date of birth etc.)

Important – Values collected through input() function isalways of String type  by default.

Look at the examples below:

X=input("Enter a number")
Y=input("Enter another number")
print(X+Y)

The output is:

Enter a number 10
Enter another number 20
10 20

Which is not desirable. Why that happened?

Simply because X and Y received the values through input() function as of type string. You already know that a Variable type is associated by the value it is holding, so X and Y are also created as String types , hence the values were concatinated instead of adding them mathamatically.

Solution

  • conversion functions for example int(), float() etc. and
  • eval() function for example to evaluate expressions

Conversion Functions

Look at the example below:

number=int(input("Enter a number "))
floatvalue=float(input("Enter a decimal number "))

# Values written in double quotes or single quotes
# are treated as strings

Val1=int("45")       
Val2=float("34.45")

Observe the example above, to see how a string (text) type is converted to desired type beause of the use of int() function.

Important – Only strings containing only valid numbers or valid float type values can be converted to their respective types.

For example the statement below will produce an error:

age=int("Gaurav Arora")

This is because “Gaurav Arora” cannot be converted to an integer type. Same holds true when we are using input() function nested inside int() function and user enters non-numeric characters.

eval() function

You can now allow users to directly enter the expressions also because eval() function can even solve expressions entered by the user in string format.

important – Here expresion has to be in string format, else t will produce an error. Observe the examples below:

result=eval("Enter a valid expression to compute: ")
print(result)

# Next few statements will produce an error

res=eval(45+3)  #Expression has to be in string format i.e "45+3"
res1=eval("hello+how")  # Not a valid expression

How to take give the output to the user?

In Python we can use print() to produce the output for the user. The exact syntax for the print() function is:

print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

Print function of Python is quite poewerful, for know (as art of our syllabus we don’t need to know all). Observe the examples below to establish how the most important features of print() function. For better understanding we are slicing down the syntax of print() function to understand one thing at a time.

print(*objects)

This simply means we can print as many values (variables, literals, expressions) in a single print statement separated by comma.

greet="Welcome"
age=17
print(greet, "your age is ", age, "next year you would be", age+1)

The output would be:

Welcome your age is 17 next year you would be 18. Observe that all values (*objects) are separated by a space.

print(*objects, sep=’ ‘)

In the example above, observe all the values are separated by a space.

That is a default behavior. Though we can change that, observe the example below:

greet="Welcome" 
age=17 
print(greet, "your age is ", age, "next year you would be", age+1,sep="#")

The output of the above code is

Welcome#your age is #17#next year you would be#18

print(*objects, sep=’ ‘, end=’\n’)

By default every print statement prints on a separate line, simply because the default value of end clause of print statement is ‘\n’ (carriage return). We can change new line separater using the end clause to make multiple print statements to print on same line seperated by the character we assign to the end clause. Observe the example below:

 

print("All these statements will print on separate lines")
print("Hello")
print("How are you?")
print("Have a good day")
print("Following lines will be printed on same line separated by dollar sign")
print("Hello", end="$")
print("How are you?", end="$")
print("Have a good day", end="$")

The output is:

All these statements will print on separate lines
Hello
How are you?
Have a good day
Following lines will be printed on same line separated by dollar sign
Hello$How are you?$Have a good day$

Please look at the example above (code and output) because it is self explainatory. Use can use any character – space, commo(,) or a full stop or period (.) in place of dollar sign.