Header Ads

Header ADS

Input - Output



Input - output in python

In this Article we will work with some built-in functions which are print( ) and input ( )

 But first understand what  basically a function is .

A function is defined as a set of code which is dedicated to some task.

> you can easily identify a function by parenthesis "   ( )  " .

each function name in  most of programming languages ends with ( ).

Now lets move to our topic  .

How to use print( ) ?

as the name suggest the print( ) function is used to print data or some value on the console screen .

Syntax :

print( data [,sep = ' ' ][,end = ' \n' ])

Note :  All the things written inside the parenthesis are called as arguments and the things under [ ] can be ignored these are optional .

the print() function can accept one or more data arguments.

       

>>> print(20)
20
>>> print("hello world")
hello world
>>> 

 

 The above code example shows how can you print data using print function .

Now lets see how can we print variable data .

       
>>> var = "Dynamic Coding"
>>> print(var)
Dynamic Coding
>>> 

Lets print string and variable data at a same time .

       
>>> var = "Dynamic Coding"
>>> print("Welcome to ",var)
Welcome to  Dynamic Coding
>>>     
 


Note : There are many ways to print variable and string data together and we will discuss them in a separate blog .


Working with sep and end arguments .

* The print() function provides a sep argument that can change the character that's used for separating strings from one space to something else .

       
>>> print(1,2,3,4,5,6,sep = ':')
1:2:3:4:5:6

 

The above code example uses the sep argument to separate the integers with semicolon ' : ' .


* The print( ) function also provides an end argument that can change the ending character for a print() function from a new line character (\n ) to something else .


Note : By default the end argument has a \n value which is known as new line escape sequence for new line .

       
>>> print("welcome to \n  dynamic coding")
welcome to 
dynamic coding
>>> 

 

The above code shows the use of \n .

       
>>> print(1,2,3,4,5,6,end='????')

1 2 3 4 5 6????
       
 

The Above code uses the end argument to put some questions marks at the end of the data.

Note :  if necessary , you can code both sep and end argument in the same function call.


input function 

input function is used to get values from the user .
the input function takes one string argument that prompts the user to enter data.
Input function pauses the program and waits for the user to enter data at the 
console. When the user presses Enter , this function returns the data entered by the user as a string  value.

using Input function with a string message is optional you can also use input function without the string message.

Syntax : val = input("Message")

INPUT :
name = input("Enter Your Name: ")

print("nice to meet you  ",name)
 
OUTPUT :
       			 
Enter Your Name: Alex

nice to meet you   Alex
 

Note : At this time we use IDLE code editor for coding.

Note : input function always returns string data, even if the user enters a number.


INPUT :
       			 
num = input("Enter a integer ")
print('you entered ',num)
print('of data type ',type(num))

 In the above code we use a function named as type( argument ).
type is a predefined function which is returns the type of data which the argument holds.

OUTPUT :
Enter a integer 123
you entered  123
of data type <class 'str'> 
 


No comments

Powered by Blogger.