In this blog, we will see how to take user input and also different formatting for outputs. Let’s start with examples.


There are two functions for inputting a varaible.

input() and raw_input(). In python 2  both will work but in Python 3 input() is only included. For this blog, I will show you the output from both but for the whole series, we are following only Python 3.

In [1]:

## Assking for inputing and cursor will stop you have to enter some value 
name = input('what is your name? ')

## After entering this particular statement will run.
print("hello" , name )

what is your name? branson
hello branson

In [2]:

## Here \t will be considered and it will add a tab on its own.

name = input('what is your name?\t')

## You can see here as well that it added some extra tab space.
what is your name?	branson

In [3]:

## Here it will consider as raw data. and add \t as string.

name = input(r'what is your name?\t')

what is your name?\tbranson

In [10]:

## It can take the number as input but you need to convert it to int in order to use it as an integer.
num = input(r'Enter a number')

## Checking the type of the inputted no.
print("Type of number is", type(num))

## Printing it as an integer using %d formatting by converting it to integer first.print("Your number is %d " %int(num))

Enter a number34
Type of number is <class 'str'>
Your number is 34

Ouput formatting

In [11]:

## Simply using print
name = input('what is your name?\t')

what is your name?	branson

In [15]:

## If we want to repeat a string string or number multiple times then we can do like this.
print('number! '*3)

print('3! '*3)

number! number! number!
 3! 3! 3!


In [19]:

name = 'Richard Branson'

In [22]:

'Your name is {}'.format(name)


'Your name is Richard Branson'

In [23]:

Country = 'USA'

In [24]:

'My name is {} and lives in {}'.format(name,Country)


'My name is Richard Branson and lives in USA'

In [26]:

## We want to change the order then can access it as follows {number}

'My country is {1} and name is {0}'.format(name,Country)


'My country is USA and name is Richard Branson'

More formatting for output

In [37]:

import math

## It will print a whole long number.
print('%f' %math.pi)

## If we want to print upto 3 decimals we can do this
print('%.3f' %math.pi)


Printing multiple values with separators

In [39]:

l = ['a' ,'b', 'c']

## It is printing in new line each entry

for e in l:


In [41]: Printing with space

for e in l:
    print(e, end =' ')

a b c

In [43]: printing with a separtor

print(*l , sep =',')


So, this is all for inputs and outputs in Python.