In Python, input and output operations (OI Operations) are performed using two built-in functions. Following are the two built-in functions to perform output operations and input operations.
- print( ) - Used for output operation.
- input( ) - Used for input operations.
Output Operations using print() function
The built-in function print( ) is used to output the given data to the standard output device (Screen).
In Python, using the print( ) function we can display a text message. The print( ) takes a string as an argument and displays the same.
print('Well Done!!')
When we use the print( ) function to display a message, the string can be enclosed either in the single quotation or double quotation.
In Python, the built-in function print( ) function is also used to display variables value. The following example shows that how to display variable value using print( ) function.
num = 120
print(num)
num = 120
print(f'The value of variable num is {num}')
In the above example code, we have used a formatted string. In Python, we can use the formatted string that is prefixed with character "f". In the formatted string, the variable values are included using curly braces ({ }).
Input Operations using input() function
The Python provides a built-in function input( ) to perform input operations. The input( ) function can be used either with some message or without a message.
When
input( ) function is used without a message, it simply prompts for the
input value. When the user enters the input value it reads the entered
value as a string and assigns it to the left-hand-side variable.
Here, the major problem is that the user does not have any information regarding what is the next step the user has to do? To solve this problem, we use the input( ) function with a message which tells the user that what the user has to do?
When input( ) function is used with a message, it prompts with a given message for the input value. When the user enters the input value it reads the entered value as a string and assigns it to the left-hand-side variable.
Notes
- Always the input( ) function reads input value as string value only.
- To
read the value of any other data type, there is no input function in
Python. Explicitly we need to convert to the required data type using
type casing.
Reading a single character in Python
The Python does not provide any direct function to read a single character as input. But we can use the index value to extract a single character from the user entered input value.
In the above example, the user has entered 'Python' as input value but the variable ch is assigned with value P' only.
Similarly, we can extract any character form the input data using the index value.
No comments:
Post a Comment