Input and Output functions in Python for beginners

Hello Friends

The most fundamental thing in Python programming language is input to program and output from the programming.

Today in this article we are going to discuss about Input and Output functions

Print()Output function

  • print() function is used to display the output
print(<obj>, ..., <obj>)

  • For example : to print string value “Demo print function” we will use print(“Demo print function”)
  • “*” operator in print() is used for repetition. For example, below code will print string “sayyam” two times
print("sayyam"*2)

  • “,” (comma) delimiter is used to separate the output. For example below code will print the output – sayyam sabadra
print("sayyam","sabadra")

  • By default print() uses space as separator
  • To use different separator we can use “sep=<separator>”, for example below code separates output using “/” so it prints – sayyam/sabadra
print('sayyam','sabadra', sep='/')

fig - 1 Python hello world program
fig – 1 Python – Demo of print() – printing Hello, World! text

input() – Input function

  • Input in a Python program is taken using the input() function
  • The input() function only accepts string/text data. This means even we enter number input() converts it into string value
  • In Python 2 we have raw_input()
input([<prompt>])

Program to enter string using input function

username=input("Enter your name:")
print("hello",username")
----------------------------------------------------
In[1]:runfile(...)
Enter your name:Sayyam
hello Sayyam

  • In the above program, the user is prompted to enter the name using input() and which will be stored in variable “username” and when the username is entered, the name is printed using print()
  • input() only accepts data in text format, but if user want to enter the numeric data, there is a need to use other function along with input() function
  • input() function stops the program execution until we hit the “Enter” key once input is given
  • Python accepts numeric input from the user in the form of an integer or float number using int() and float() functions

Program to demonstrate int() and float() functions

amount= int(input("Enter the amount: "))
print("The amount is:",amount)

profit=float(input("Enter the profit:",))
print("The profit is:",profit))
------------------------------------------------------
In[1]:runfile(...)
Enter the amount: 3000
The amount is: 3000

Enter the profit: 500.67
The profit is: 500.67

In[2]:runfile(...)
Enter the amount: 450
The amount is: 450

Enter the profit: 40
The profit is: 40.0

In above program we are taking amount which is numeric so we are using int() to convert into number from string

Similarly profit is in decimal values so to convert entered float value we are using float()

We have very good articles on Python, please have a look once to explore more on python – https://knowledge-junction.com/?s=python

Thanks for reading 🙂 if its worth at least reading once, kindly please like and share 🙂

Have a nice day 🙂

You may also like...

1 Response

  1. Yogesh Meher says:

    Very nice article Sayyam

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: