Input and Output in Python
There will be situations where your program has to interact with the user. For example, you want to take input from the user and then display some results. We can do this by using the input() function and the print() function, respectively.
print()
The easiest way to produce output is to use the print () function where you can pass zero or more comma-separated expressions. This function converts expressions that you pass into a character string before writing on the screen.
We can also export data to a file, but we will discuss this later.
Syntax
print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=False)
- value(s) : Any value, and as much as you want, will be converted to a string before being displayed on the screen.
- sep = ’separator’: (Optional) Specify how to separate objects if there is more than one. Default: '' (empty)
- end = ’end’: (Optional) Specify what to display at the end. Default: '\ n' (line break)
- file: (Optional) An object with a writing method. Default: sys.stdout (consol)
- flush: (Optional) A boolean, specifying whether the output is flushed (True) or buffered (False). Default: False
- Retour: returns the output to the screen.
Example 1 :
a=2;b=5; tab=[2,3,5] # print a message print("bonjour") # print a variable value print(a) # print a message and also a variable value print("bonjour ", a) # print many elements print("bonjour ",a,b) # use a separator between values print("bonjour ",a,b,sep=" & ") # use an end character of the string for elm in tab: print(elm, end=' : ')
2
bonjour 2
bonjour 2 5
bonjour & 2 & 5
2 : 3 : 5 :
Output formatting
Sometimes we would like to format our output to make it look pretty. This can be done using the str.format() method. This method is visible for any string object.
Example 2 :
>>> a=4;b=12 >>> print("la valeur de a= {} et b= {}".format(a,b)) la valeur de a= 4 et b= 12
Here, the braces {} are used as placeholders. We can specify the order in which it is printed using numbers (tuples index).
Example 3 :
>>> a=4;b=12 >>> print("la valeur de a= {1} et b= {0}".format(b,a)) la valeur de a= 4 et b= 12
We can even use keyword arguments to format the string.
Example 4 :
>>> a=4;b=12 >>> print("{msg} a= {val1} et b= {val2}".format(val1=a,val2=b, msg="la valeur de ")) la valeur de a= 4 et b= 12
We can even format strings like the old sprintf() style used in the C programming language. We use the operator % to accomplish this.
Example 5 :
>>> b=34.534534534 >>> print("b = %.2f " %b) b = 34.53
input()
Until now, our programs have been static. The value of the variables was defined or hard coded in the source code.
To allow flexibility, we may want to take user information. In Python, we have the input () function to allow this. The syntax for input () is as follows:
Syntax :
input([msg])
where msg is the string we want to display on the screen. It is optional.
Example 6 :
>>> a=input("Type a value : ") Type a value : 5 >>> a '5'
Here we can see that the value entered 10 is a string, not a number. To convert this to a number, we can use the int() or float() functions.
Example 7 :
>>> a=input("Type a value : ") Type a value : 5 >>> int(a) 5
Example 8 :
>>> a=int(input("Type a value : ")) Type a value : 5 >>> a 5
0 Comment(s)