This article focuses on two Python built-in functions for performing I/O tasks: print() and input(). You’ll also learn how to import and use modules in your code.
Python has a lot of built-in functions that we may use right from the Python prompt.
Some functions, such as input() and print(), are commonly used for standard input and output operations. Let’s start with the output section.
Python Output Using print() function
To output data to the standard output device, we use the print() method (screen). We can also save data to a file, but we’ll get to that later.
An example of its use is given below.
print('This is an article')
Output
This is an article
Another example is given below:
c = 5
print('The value of c is', c)
Output
The value of c is 5
We can see that a space was placed between the string and the value of variable c in the second print() instruction. This is the default, but we may modify it if we want to.
The actual syntax of the print()
function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here, objects
is the value(s) to be printed.
The sep
separator is used between the values. It defaults into a space character.
After all values are printed, end
is printed. It defaults into a new line.
The file is the object that prints the values, and the default value is sys.stdout (screen). Here’s an illustration of what I’m talking about.
print(1, 2, 3, 4)
print(1, 2, 3, 4, sep='*')
print(1, 2, 3, 4, sep='#', end='&')
Output
1 2 3 4 1*2*3*4 1#2#3#4&
Output formatting
We may want to format our output to make it more appealing. The str.format() method can be used to accomplish this. Any string object can see this function.
>>> x = 5; y = 10
>>> print('The value of x is {} and y is {}'.format(x,y))
The value of x is 5 and y is 10
Curly braces are utilized as placeholders in this case. We can use numbers to specify the order in which they are printed (tuple index).
print('I love {0} and {1}'.format('code','codelivly'))
print('I love {1} and {0}'.format('coder','programmer'))
Ad
Output
I love code and codelivly I love coder and programmer
We can even use keyword arguments to format the string.
>>> print('Hello {name}, {greeting}'.format(greeting = 'Goodevening', name = 'Rocky'))
Hello Rocky, Goodevening
We can also format strings in the same way as the old sprintf() function in the C programming language did. To accomplish this, we employ the percent operator.
>>> x = 12.3456789
>>> print('The value of x is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %3.4f' %x)
The value of x is 12.3457
Python Input
Our programs had been stable up until now. Variable values were either defined or hardcoded into the source code.
We could wish to take the user’s input to provide for more flexibility. The input() method in Python allows us to do this. input() has the following syntax:
input([prompt])
where prompt is the text we want to see on the screen. It’s a choice.
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
The entered value 10 is a string, not a number, as can be seen here. We can use the int() or float() functions to convert this to a number.
>>> int('10')
10
>>> float('10')
10.0
The eval() function can be used to achieve the same task. But eval goes a step farther. Even expressions can be evaluated if the input is a string.
>>> int('2+3')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5