What is the use of programming if users cannot interact with the program? So learning input output with program is fundamental. Python programming language lets users achieve such input output interaction via the print and input statement.

Print is used to display strings and data to the user in the output display monitor. Input function is used to get data and strings from users. So these two are basics in python programming language.

Let's see print( ) examples.

>> print("Hello world!")
Hello world!

This is the most simple print() statement. But print statement are not limited to only such basic outputs. print statement comes with various build in arguments that allows us to specify or manipulate output. For example there is sep and end arguments which allows us to seperate contents using any character we pass into and end arguments allows us insert any character after the content.

For example.

Hello world

print("Hello","world", sep=",")

print("Hello","world", end="#")
Hello world#

The most commonly required print statement is perhaps using the string format operator. Suppose that you have calculated some variable in your program and want to display that variable in the output along with some message. Herein, the string formatters with % operator is commonly used..

For example:

age = 25
print("The age is %d" %age)
The age is 25

Notice that there is no need for comma between the string part and the variable part like the one below:

print("The age is %d", %age)
  File "<ipython-input-77-93554dfd41fb>", line 1
    print("The age is %d", %age)
SyntaxError: invalid syntax

The %d indicates that we want to print out decimal. We can also use %f for float number display, %e for exponential format, %u for unsigned etc.

If we want to print string such as names then we can use the %s formatter.

name = "Jon"

print("The name is %s" %name)
The name is Jon

Now what if we want to display more than one variables. In this case we use the formatters whereeve they are required and in the variable passing argument we use the brackets ( ) as shown by the following example.

name = "Jon"
age = 25
print("The name is %s and age is %d" %(name, age))
The name is Jon and age is 25

But one should know that we could also have the above examples as follows.

print("The name is ",name,"and age is ",age)
The name is  Jon and age is  25

Here comma was required to insert the variables in their places.

The above examples we have used basic examples and they are almost all that one needs to know.

But we can also have print statement in iterative loops like for loop or while loop. There is nothing special but to show how they look like when used therein we show some examples.

mylist = ["apple", "banana", "mango"]

for items in mylist:


The last 3 lines are actually the output of the print.


Post a Comment