Python Input and Output 

Some functions are used for input / output in Python.

'Input ()' this function is used to take input from the user's keyboard and 'print ()' this function is used to print something on the user's screen.


input () function for taking User Input

In Python, when the user comes to take some letters or numbers, then the 'input ()' function is used.


Syntax for input () function

input ("SomeText")



Parameter:

SomeText: 

Some text is given here about what to input to Optional.User.


Example for 'input ()' function

Source Code:

a = input ("Enter your name:")
print ("Your name is", a)


Output:


Enter your name: Babita
Your name is Babita


Intput()


When User; numeric, letters or special symbols; When we take input, its data type is 'string'.


Source Code:

a = input ("Enter Numbers:")
print ("Entered input's type is", type (a))


Output:

Enter Numbers: 7985
Entered input's type is <class 'str'>


string


Type conversion

When that String data type has to be converted to Number (int, float, complex number), then Type Conversion functions have to be used.


Source Code:

a = input ("Enter Number:")
print ("Entered input's type is", a)

b = int (a)
print ("Entered input's type is", b)
print ("Entered input's type is", type (b))

c = float (a)
print ("Entered input's type is", c)
print ("Entered input's type is", type (c))

d = complex (a)
print ("Entered input's type is", d)
print ("Entered input's type is", type (d))


Output:


Enter Number: 7
Entered input's type is 7
Entered input's type is 7
Entered input's type is <class 'int'>
Entered input's type is 7.0
Entered input's type is <class 'float'>
Entered input's type is (7 + 0j)
Entered input's type is <class 'complex'>



type conversion



print () Function for Displaying Output on Screen

Till now the 'print ()' function was used to show the output on the user's screen, but there is more than one parameters for the print () function. Which are used to give more than one newline or for file handling.


Regular Example for print () Function

a = 7
print ("Value of a is", a) #

Output: 

Value of a is 7


print()



Syntax for print () Function

print (val1, val2, ..., valN, sep = "", end = "\ n", file = sys.stdout, flush = False)



Parameter:


val1, val2, ..., valN,: Here one or more values ​​are given. Each value is seperated by comma (,).


sep = "": 

Optional. When more than one values ​​are given, space () is given by default 'sep' parameter. User can give any value on 'sep' parameter.


end = "\ n":

 Optional. The '\ n' (newline) default is given on the 'end' parameter. User can give any value on 'end' parameter.


file = sys.stdout: 

Optional. The default 'sys.stdout' (print entered text on screen) is given on the 'file' parameter. User can open any file on 'file' parameter.


flush = False: 

Optional. False is the default value on the flush parameter. The stream is not flushed when False is given or the stream is forcibly flushed when 'True' is given.

print () function returns null / None.


Example for print () function with 'sep' parameter

'!!!!' Is separate from this string.


Source Code:


print ("Hello Friends", "Hello  Babita", sep = "!!!!")

Output:


Hello Friends !!!!! Hello Babita


sep parameter


Example for print () function with 'end' parameter

In the example, this string is given at the end '\ n \ n \  \ n'.


Source Code:


print ("Hello Everyone", end = "\ n \ n \ n \ n \ n \ n ")
print ("Hello Friends")

Output:


Hello Everyone




Hello Friends



Example for print () function with 'file' Parameter

'textfile.txt' by 'file' parameter of print () function has written this string 'Hello World' on this file.


Source Code:

f = open ("textfile.txt", "w")
print ("Hello World", file = f)
f.close ()

textfile.txt


Hello World