Input and Output functions
Input and output functions
input ( )
It refers to the data whatever we enter or the data entered by a user. Python supports two built-in functions for reading input namely input( ) supported in the latest versions and raw_input( ) supported upto python 2.x versions.
Ex:
>>> input( ) #inbuilt function
A python programmer ‘A python programmer’ >>> var=input(“Enter a number:”) #giving integer as input Enter a number: 5 >>> var1=int(input(“The number is:”)) #another way to give integer as input The number is: 7 >>> x=str (input(“Your string is:”)) #Giving string as input Your string is: orange
>>> v1=input(“you entered:”) you entered: 16.13 >>> print(v1) 16.13 |
print( ) statement
To print anything you need print( ) statement. The usage of print( ) is explained in the below examples:
>>> a=5
>>> print(a) 5 >>> a=”5″ >>> print(a) 5 >>> a=”A Print function” >>> print(a) A Print function >>> print(‘a’) #using single quote a >>> print(“a”) #using double quote a >>> “hi” ‘hi’ >>> ‘hi’ ‘hi’ |
Python uses some command operators to print. Following are some of the examples.
>>> print (‘V’+’M’+’S’) #’plus’ for concatenating two strings into a single string
VMS >>> print (“V”+”M”+”S”) VMS >>> print(“520″+”+”+”6”) 520+6 >>> print(“520″+”+”+”6″+”=”+”526”) 520+6=526 >>> print(“VMS,”+”Welcomes you all”) VMS,Welcomes you all
>>> print(“A”,”Step”,”To”,”Learn”) # ‘comma’ for printing multiple strings in a line A Step To Learn |
Note: If you want to print multiple lines/a paragraph you can use ’’’ (triple quotes)
>>> print(“hi\nhello”) #here \n is to print on new line or next line
hi hello >>> print(“Hi\nI am Sudhakar\nWelcome to all”) Hi I am Sudhakar Welcome to all
>>> print(“coding\tis\teasy”) # here \t is used to print tab separated line coding is easy >>> print(“coding\tis easy”) coding is easy |