Increases font size in python and how does the number of decimals decrease?

Asked

Viewed 3,049 times

1

print ('Área da seção transversal:',Acf,'m^2 \n')
print ('Velocidade média:',Vf,'m/s \n')
print ('Número de reynolds:',Re_f,'[adimensional] \n')
print ('Número de nusselt:',Nuf,'[adimensional] \n')
print ('Coeficiente de troca de calor por convecção:',hf,'W/m^2 K \n')
print ('Capacidade térmica:',Cf,'kW/K \n')

Cross-section area: 1.2566370614359172e-05 m 2

Average speed: 7.976571864194266 m/s

Number of Reynolds: 23823.25541992501 [dimensionless]

Nusselt number: 15.622791714636927 [dimensionless]

Convection heat exchange coefficient: 2348.2618226270765 W/m 2 K

Thermal capacity: 0.41812800000000006 kW/K

  • https://stackoverflow.com/questions/2075128/python-print-all-floats-to-2-decimal-places-in-output I believe this answers your question to the decimal places.

1 answer

4

font size

Increasing the font size is not possible for terminal output programs (with print): This is part of your terminal settings - which is a program that your Python program, and the Python language executable itself, only uses, it is not "owner" of the terminal.

Programs that interact with "world" with print and input only come three "data streams", which are the (pseudo) sys.stdin, sys.stdout and sys.stderr files - all print does is write characters in those files.

In operating systems that are not Windows, or, with some configuration in Windows, some strings of characters allow the configuration of front and background colors, as well as text positioning on the terminals - (see https://en.wikipedia.org/wiki/ANSI_escape_code ), but there are no sequences to change the font size.

If you want a more user-friendly output, perhaps as part of the "finish" to deliver an application for use by other users, the only output is to create a program that uses another type of interface than the terminal. It could be a program with a web interface - in this case you could use a micro-framework like the "Flask", or a program that works with a graphical window directly - in this case you can use the "Tkinter", that accompanies the installation of Python in Windows, or a graphical Toolkit like the "Qt".

Limit decimal places

This is simpler - the problem is that you are using the print in a very rudimentary way - separating everything you want to print by ",", and delimiting each substring by quotation marks.

The most common use is to use Python’s string formatting capabilities and compose a single string with the already formatted text and data that you want to print, and then call the print with that string. In case the program has been changed to a graphical window, or an output to the Web, the same string can be used, only it is not passed to print.

Starting from Python 3.6, the recommended way to compose strings with text and formatted data is with the use of "f-strings" - are strings that have the prefix f" or f' - that is, the letter "f" before the opening quotation marks. Inside these strings, any expression that appears between keys ({ }) is treated as Python code (for example, a variable name is replaced by its value). And even more, before closing the key, after the desired expression one can make use of a "mini formatting language", which allows things like justifying the text or controlling the number of decimals of a number.

The mini-language is complicated, but going to the part that matters - you can write your print thus:

print(f'Coeficiente de troca de calor por convecção: {hf:0.04f} W/m² K') 

Thus, as explained - within the same string, started with the prefix f, the name hf used inside the keys will be exchanged for the variable value, and the sequence :0.04f indicates that the value must be treated as a floating point, and 4 numbers must be printed after the decimal point, filled with "0" if there are fewer decimal places than that.

The full reference of the formatting mini-language is the one documented for the method format of strings (which should be used in versions prior to 3.6 of Python, which did not yet have the f-strings) - https://docs.python.org/3/library/string.html#format-string-syntax

(I also switched the 2 for the "²" character - it will work on most terminals - but maybe not on the standard Windows terminal).

Jupyter notebook

In simple use, the Jupyter notebook emulates the behavior of sys.stdin, sys.sdout, so that the print and input work. Using the normal Python functions it is not possible to have more control over the output than in a terminal. The bigger difference is that unlike a Windows terminal, it allows some ANSI sequences, in particular for color change. (Short example is in the comment below).

However the notebook jupyter has some categories of objects that allow more control of the output, and interaction with the direct browser yes - that could limit the development of a complete graphical application inside the notebook.

However, doing this is a couple of orders of magnitude more complicated than simply using "print" and "input". The most direct way seems to be to create an object that will be an output "widget", and using this "widget" in a command with from Python, you can redirect the print output to it. The Widget on the other hand can have some of the custom printing features - it can have border and color of text specified in CSS. However, I was unable to amend the font size: the "font-size" property is ignored in the widget layout.

from ipywidgets import widgets
from IPython.display import display

out = widgets.Output(layout={"color":"red", 'border': '1px solid blue', 'font-size': '200%'})

display(out)
with(out):
    print(f"Alô mundo. Formatando número: {3.210002:0.02f}")

Documentation of widgets: https://ipywidgets.readthedocs.io/en/stable/examples/Output%20Widget.html

  • To make this code I am using Jupyter Notebook, I am still beginner in this area of programming and this information will help a lot. About increasing the print output source was to be able to better visualize the results in the results area of this program, in Jupyter it would be possible to increase the font size of the strings in the output?

  • No - from the point of view of Python, jupyter also functions as a terminal - the part that is in the browser uses CSS and HTML, of course, but this is within the code of jupyter itself - a program, or code in Pyton to run inside the notebooks does not have access to them. However, the notebook jupyter implements in a limited way the ANSI sequences that I mentioned - then, printing the sequence "<ESC>[<color code>m" is possible to have the output of the color print on the notebook: print("\x1b[31malo") - prints "hello" in red.

  • I updated the answer to include some considerations about jupyter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.