Python Input Output

Input & output

  Machine Learning in Python



There is just one function in Python that is used to take input from the user and we have already seen it in the previous sections. The signature of the function is pretty simple

input( "prompt >>")

So, for example, if you wanted to get the age of the user,

age = input( "enter age - ")

The tricky thing that we have to know here is that the input ( ) function returns a string. So, in cases like this, you would have to explicitly convert it into the data type that you want.


We know that the user has entered his or her age. So, convert it into integer.

age = int(age)



Output on the other hand is a bit more involved. The primary function for text output is the print ( ) function. The signature of the print function is shown below.

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

The default separator is space and the default end of line is a new line character.

print("Hello", "World") # Observe the space between "Hello" and "World"

Hello World

print ( "Hello" )
print ( "World" ) # Observe the fact that the second print statement is printed on a new line.


You can modify the default behaviour by specifying a separator or an end of line yourself.

print ( "Hello","World",sep="-")


print ( "Hello", end="")
print ( "World")

objects ofcourse are any python objects. They are stringified before being printed out. In order to do that, Python calls the str ( )method which essentially calls the str method of the object. So, for example, if the Account class ( that we have seen in the previous sectino ) had a str method, it would be called when we use that class in a print statement.

class Account :
    def __init__ (self,number, name, balance) :
        self.account_number  = number
        self.account_name    = name
        self.account_balance = balance
    def __str__(self) :
        return "Account " + str(self.account_name) + "'s balance is " + str(self.account_balance)

acc = Account(1001,"Ajay Tech", 2100)
print ( acc )

Account Ajay Tech's balance is 2100

file and flush are beyond the scope at this point. So, let’s leave them for now.

String formatting

The built-in str class has a format ( ) function that can be used to format strings. It is especially useful to pretty print strings. Here is a simple example.

text = "Ajay Tech is {} years old"
text_f = text.format(2)

print ( text_f)
Ajay Tech is 2 years old

This is just a very simple example of using the format function. There are many variations of this. We will just see a few examples. You can see more variations of Python’s format function here.

"{} is a {} company".format("Ajay Tech","technology")

'Ajay Tech is a technology company'
"{1} is a {0} company".format("technology","Ajay Tech")
'Ajay Tech is a technology company'

You can even use elements from a list or other complex data structures. Everything outside of the { } will be treated as string literals.

Another example is to replace by argument name.

"{company_name} is a {type} company".format(type="technology",company_name="Ajay Tech")

'Ajay Tech is a technology company'

Another useful feature is alignment. For example, if you wanted to print a list of numbers neatly aligned to the right so that it is easy to read, use the following format syntax.

population = {"india"    :1367097934,
              "china"    :1419518156,
              "US"       :328830848 }

for key in population.keys() : 
    print ("{:<10} - {:>10}".format(key,population[key]) )

india      - 1367097934
china      - 1419518156
US         -  328830848

Especially look at the right alignment of the numbers making it much more readable

You can even format number with commas at their thousands

"The population of India is {:,}".format(1367097934)
'The population of India is 1,367,097,934'

There are many more formatting options available with the format ( ) function. You can view examples of most of these formatting options here.

%d bloggers like this: