Python Input and Output Statements

Input : Any information or data sent to the computer from the user through the keyboard is called input.

Output: The information produced by the computer to the user is called output.

Python provides us with the two inbuilt functions as input() and output().

The syntax for input is input(prompt_message); the python will automatically identify whether the user entered a string, number, or list; if the input entered from the user is not correct, then python will throw a syntax error.

And the syntax for the output in python is print(), normally used to print the output.

  • Python-2 supports the raw_input() function and input() function
  • Python-3 supports only for input() function

Let us see the difference between the raw_input() and the input() function:

raw_input():

input():

The Return type of raw_input is always a String type The return type of input() is always the same datatype
syntax: x=raw_input()
syntax: x=input()
Even if you input the integer value also, raw_input() will return the string datatype only If the input type is integer data type, then it will return the integer value only
If the input values are
string type then raw_input() will return the string datatype only
If the input data type is string, then it will return the string value only
Python 2.x will support raw_input() and input() Python 3.x will support input()

Following example will read two inputs from the keyboard and print the sum

x=input("Enter First number:")
y=input("Enter Second Number:")
i=int(x)
j=int(y)
print("The Sum:", i+j)

Or else even we can simplify the above program:
x=int(input("Enter Fisrt number:"))
y=int(input("Enter Second Number:"))
print("The Sum:", i+j)

python-input-output

Let us consider another one example to know how we can use the input() function. The Following example read the employee data from the keyboard and print that data.

eno=int(input("Enter the Emplyoyee number:"))
ename=input("Enter the Employee name:")
esal=float(input("Enter the employee salary:"))
eaddr=input("Enter the employee address:")
married=bool(input("Is employee married?[True|False]:"))

print("Please confimr your provided information")
print("Emplyoyee number:", eno)
print("Employee name:", ename)
print("Employee salary:", esal)
print(" Employee address:", eaddr)
print("Employee married?:", married)

python-input-output-one

In the below example, I am going to give, Married status as False but still, python is printing the output as True only; this happens because we are converting the string into Boolean Datatype.

Python gives False while converting the string into a boolean, only in the non-string or empty string case.

python-input-output-two

But here whether the value is True or False python will consider the value as string data type and print True only; so to overcome this problem we have to use the Eval Function instead of Bool Function.

Command Line Arguments

eval() Function

The return type of eval() function is automatically considered based on what argument is passed to eval() function.

If the argument represents a string, then eval() function also returns a string. If you pass an argument as int, float, string, boolean, it will return the corresponding datatype only. The eval() function is used as an alternate for typecasting.


Let us see some example for eval()

x=eval(input("Enter something:"))
print(type(x))

eavl-one

eval() with float value

eval-two

eval() with boolean value

eval-three

eval() with string value

eval-four

eval() with list

eval-five

eval() with tuple value

eval-six

And you can pass the expressions

x=eval('10+56+23')
print(x,type(x))

eval-seven

So let us use the eval() function in the employee data program and execute the program

eno=int(input("Enter the Emplyoyee number:"))
ename=input("Enter the Employee name:")
esal=float(input("Enter the employee salary:"))
eaddr=input("Enter the employee address:")
married=eval(input("Is employee married?[True|False]:"))

print("Please confimr your provided information")
print("Emplyoyee number:", eno)
print("Employee name:", ename)
print("Employee salary:", esal)
print("Employee address:", eaddr)
print("Employee married?:", married)

python-input-output-three

python-input-output-four

Now, let us understand how to read multiple values from the keyboard in a single line

a,b=[int(x) for x in input("Enter two numbers:").split()]
print("The Sum:",a+b)

python-input-output-five

In the above program, when the user enters two values/numbers, the python will consider those values as one single string, but what we are looking for is two numbers/values. To retrieve those two values, we need to split the string.

int(x): I have used it to convert the split string into an int

Split () function breaks a single string into multiple strings using a delimiter. By default, split() function considers space as delimiter.

 input("Enter two numbers:").split()

And if you wanted to split the numbers with a comma, then you can pass the argument as split(",")

a,b=[int(x) for x in input("Enter two numbers:").split(",")]
print("The Sum:",a+b)

python-input-output-six

In our next example, we are going to read three inputs from the keyboard with comma separation and print the Sum.

In this example also I am going to include all the operations in a single line; let me explain to you line by line.

a,b,c=[float(x) for x in input("Enter the three values with comma separation:").split(",")]
print("the sum:",a+b+c)

First, I am going to enter the three float values as an input, and here I am passing the split() to separate those three values with comma as split(",").

input("Enter the three values with comma separation:").split(",")

Next, for each x in input() function, convert into float datatype, so I am passing

[float(x) for x in input("Enter three values with comma separation:").split(",")]

And next, I am going to assign these values into three variables

a,b,c=[float(x) for x in input("Enter three values with comma separation:").split(",")]

Now, print the sum of these three variables

a,b,c=[float(x) for x in input("Enter three values with comma separation:").split(",")]
print("The Sum:",a+b+c)

And Hence the output is

python-input-output-seven

else statement

Python Output statements

The python output() is used to print the output for the end-user by taking the input from the keyboard. The only possibility to print the output is by using the print() statement.

There are multiple forms to print this print statement

  • Printing the print statement without any arguments. Let us see how it will work
    print("chercher")
    print()
    print("tech")
    ​
    So if we try to print the print() statement without any arguments, then it will add an empty line between the two outputs.
    output-function-one

While printing the string arguments, we can use the escape characters like newline command( ), tab command ( ) in between arguments.

print("cherchertech")
print("chercher
tech")
print("chercher	tech")​

output-function-two

  • In the same way, we can also use the + operator to perform concatenation and Plus(+) operator to repeat the string. But the important thing is that, while using the + operator, both the arguments must be string only and while using the star(*) operator, one argument must be of int type, and another must be of a string data type.
print('chercher'+'tech')
print(5*'chercher.tech')
​

output-function-three

Next, a print statement with any number of arguments is acceptable, in the following example I am going to pass four arguments with comma separation, accepting a variable number of arguments is one of the beautiful features of the print() statement.

a,b,c=10,20,30
print("values are:",a,b,c)​

output-function-four

Python string Datatype

The Separator between the arguments in print () statement by default is the space in python, by using the sep parameter, we can change space into any character, integer, or string type.

The sep attribute supports only in python 3.x versions; we can also format the output by using the sep attribute.

The syntax for sep attribute is as follow:

a,b,c,d=10,20,30,40
print(a,b,c,d,sep=":")

sep-attribute-three

In the above example, I have mentioned sep attribute as colon(:), you can choose which separator you want based on your requirement.

#for formatting a date
print('20', '01', '1992',sep="/")

sep-attribute-one

For disabling the soft space between the arguments, we can use an empty string like this "."

print('A', 'B', 'C', sep="")

sep-attribute-two

Accessing the list elements by using the index and slice operator

The print() statement with end Attribute

Every print() is going to print the output in a newline; by using the end attribute, we can make the output to print in a single line.

The default value of the end attribute is newline( ), let us see how we can make use of end attribute in printing the output by using the print() statement.

print('cher',end='')
print('cher',end='')
print('.tech',end='')

end-attribute-one

Instead of soft space, we can also make use of a colon(:), an asterisk(*) and at the rate symbols, etc..

end attribute by using the @ symbol

print('chercher.tech', end="@")
print('gmail.com',end='')

end-attribute-two

End attribute by using colon(:)

print('ABCD',end=':')
print('EFGH', end='')

end-attribute-three

end attribute by using the $ symbol

print('INDIA', end='')
print('INDIA', end='$')

end-attribute-four

Using the sep attribute with end attribute

print(10,20,30, sep=':',end="::")
print('integer values')

end-sep-attributes

Difference between Finally Block and Destructor

The print statement cannot take string only as its arguments; instead, we can use any arguments with the print() statement.

Below examples showcase you different types of arguments with print() statements

L=[10,20,30]
print(L)
# the output for above code is
[10,20,30]

T=(1.0,20,23)
print(T)
#The output for above code is
(1.0,20,23)

python Interfaces

Print() statement with Replace() Function

The replace () is an inbuilt function of python, where replace() is going to returns the copy of a string with the substring where we had specified. The replacement operator is specified with the {} symbol.

The syntax for replace operator is:

string.replce(old, new, count)

The parameters are:

  • old: old substring which you want to replace
  • new: new substring which is going to replace the old substring
  • count(optional): The number of times you wish to return the old substring with unique substring.
    The following example demonstrates the use of replacing (), operator
    example:1
    s="Hello chercher technology"
    #print the string by replacing technology with tech
    print(s.replace("technology","tech"))​

    replcement-operator-one

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions