Curriculum
In this lesson, you will learn about input and output statements in Python and how to use them in your program.
In any programming language, it is important to have input and output statements to perform high-level operations.
Python has some built-in functions to accept data from the user and to display the result of any particular operation that the user defines. Basically, it’s the input and output statements of python.
Let’s learn about them in detail.
To accept data from the user, we use the input function. Python has the built-in function input() to accept data from the user.
Syntax
input(statement)
When data is read through the input statement, the python compiler interprets it as a string. Yes, string (str) is the default input statement in python.
To have a number as the input value, we can use int, float functions prefixing the input function.
a= int(input(statement)) b=Float(input(statement))
Similar to the input function, python has a function to display the result of an operation. It is the print() function.
Syntax
print(‘statements’)
Inside the function, if the statement is enclosed by quote(s) then it is the string that’s printed.
Example:
a=57 print("The value of ‘a’ is:", a)
Output:
The value of ‘a’ is: 57
We can format the output by using the string.format() function. It is normally used when there is a need to print the values in between strings. The places of the values are defined using {}.
Example:
a=str('Developer Publish Academy') b=str('string formatting') print(f'Welcome to {a}, you are learning {b}')
Output:
Welcome to Developer Publish Academy, you are learning string_formatting
So now, are you aware of python’s input and output statements? Then, now let’s put them into action
Sample program:
Input
a=input('I learn python programming from :') print(a) b=int(input('Enter the value of b:')) c=float(input('Enter the value of c:')) d=(c+b) print(f'The addition of two values is :{d}')
Output:
I learn python programming from :Developer Publish Academy Enter the value of b: 57 Enter the value of c:75.0 The addition of two values is :132.0
Learn more about python programming and other programming languages here.
Try out your program without installing a compiler at CodersEditor