3. Input, priting, and formatting#

Saleh Rezaeiravesh and Aditya Mishra
saleh.rezaeiravesh@manchester.ac.uk
Department of Mechanical and Aerospace Engineering, The University of Manchester, Manchester, UK


Overview: In this notebook, we briefly review the input from keyboard and printing to screen. For the latter, we also cover formatting for different data types.

3.1. Printing to screen#

As you have already seen, the most basic way of displaying an output is through using print statement. What is going to be printed must be a string:

print("This displays in your terminal")
This displays in your terminal

If there are multiple strings, they can be concatenated by a comma:

print("This displays in your" , "terminal")
This displays in your terminal

3.1.1. Formatting#

As we said, we can only print strings. What if we want to print integers, float, etc? An easy way is to use formatting placeholders for the variables to appear as string. This can be done using the String Modulo Operator (%), see the following table.

Data type

Formatting type

Operator

integer

Standard

%d

integer

Hexadecimal

%x

integer

Octal

%o

float

Standard

%f

float

“Optimal” notation

%g

float

Exponential notation

%e

string

Standard

%s

Example: Print an integer number using various formatting:

a = -159

print("a as integer = %d" %(a))
print("a as float = %f" %(a))
print("a as optimal float = %g" %(a))
print("a as exponential float = %e" %(a))
print("a as string = %s" %(a))
print("a as hexadecimal = %x" %(a))
print("a as octal = %o" %(a))
a as integer = -159
a as float = -159.000000
a as optimal float = -159
a as exponential float = -1.590000e+02
a as string = -159
a as hexadecimal = -9f
a as octal = -237

Example: Print an float number using various formatting:

b = -159.843962

print("b as integer = %d" %(b))
print("b as float = %f" %(b))
print("b as optimal float = %g" %(b))
print("b as exponential float = %e" %(b))
b as integer = -159
b as float = -159.843962
b as optimal float = -159.844
b as exponential float = -1.598440e+02

Note: We can also specify the width and the precision, i.e. the number of digits after the decimal point for float.

For instance for 5.2%f or 5.2%e:

  • 5: is the minimum width of the entire formatted value. If the value takes fewer characters, spaces will be added on the left for padding.

  • 2: sets the precision to 2 meaning that maximum 2 digits will be displayed after the decimal point. An auotmatic round off is applied based on the dropped digitis.

print("b as float 5.2 = %5.2f" %(b))
print("b as float 5.3 = %5.3f" %(b))
print("b as exponential float = %5.2e" %(b))
print("b as exponential float = %5.3e" %(b))
b as float 5.2 = -159.84
b as float 5.3 = -159.844
b as exponential float = -1.60e+02
b as exponential float = -1.598e+02

3.2. Keyboard Inputs#

To recieve keyboard inputs from your terminal, we use the input built-in function of Python. The input function reads the user’s input and returns it as a string. For example:

a = input("Enter your input: ")

print("Recieved input is: ", a)
print(type(a))
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
Cell In[6], line 1
----> 1 a = input("Enter your input: ")
      3 print("Recieved input is: ", a)
      4 print(type(a))

File ~/The University of Manchester Dropbox/Kristaps Stolarovs/repos/numericalPythonBook/.venv-home/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt)
   1279 if not self._allow_stdin:
   1280     msg = "raw_input was called, but this frontend does not support input requests."
-> 1281     raise StdinNotImplementedError(msg)
   1282 return self._input_request(
   1283     str(prompt),
   1284     self._parent_ident["shell"],
   1285     self.get_parent("shell"),
   1286     password=False,
   1287 )

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

If the input is a string, this will work perfectly. But what happens if the input is an integer or a float? In this case, we need to convert the input value from string to the right type.

  • To convert the input to an integer, use int().

  • To convert the input to an float, use float().

a = input("Enter a number: ")

print("Original type:", type(a))

a1 = float(a)   #conversion from string to float
print("Converted value:", a1)
print("Converted type:", type(a1))
Enter a number: 3.56
Original type: <class 'str'>
Converted value: 3.56
Converted type: <class 'float'>

Now the value is converted to a float, we can use it in mathematical operations. For instance,

print(a1**2.)
print(1./a1)
12.6736
0.2808988764044944

If we have multiple inputs, there are two options:

  • Option 1: Repeat the input command multiple times, each time reading the value of one of the inputs.

  • Option 2: Use the input command only once, but use split().

#Option 1
a = input("Enter values of a: ")
b = input("Enter values of b: ")
print(a,b)
Enter values of a: 2.8
Enter values of b: -6.9
2.8 -6.9

Use space to separate in the input values:

#Option 2
a, b = input("Enter values of a and b: ").split()
print(a,b)
Enter values of a and b: 2.3 6.5
2.3 6.5