Here are a few examples of using print() in Python. I encourage you to do more research and practice on your own. Of course you can copy and paste the code into your own environment. I’m using Jupyter Notebook.
The output is:
1 | print ( 'My name is {} and my number is {}.' . format (name,x)) |
Here is the output:
1 | My name is Bob and my number is 7. |
2 | print ( 'My name is {one} and my number is {two}.' . format (one = name,two = x)) |
Here is the output:
1 | My name is Bob and my number is 7. |
1 | print ( '{:.2f}' . format ( 123.456 )) |
Here is the output:
1 | print ( '{:.2f}' . format ( 123.456 )) |
Here is the output:
1 | print ( '{:.1f}' . format ( 123.456 )) |
Here is the output:
1 | print ( '{:.2%}' . format ( 0.451 )) |
Here is the output:
Here’s a good use case for formatting a number as a percent. We have a dataset as a pandas DataFrame called df. We wnat to know the percent of rows that are duplicated. Here how we can do that and put the number into a string. Notice that the first element of shape gives us the number of rows in the dataset. The second element represents the number of columns in the dataset.
1 | perc = df0.duplicated(). sum () / df0.shape[ 0 ] |
2 | print ( '{:.2%}' . format (perc) + ' is duplicated' ) |
If you want to print some of your text in bold, have a look at this article called How to print Bold text in Python [5 simple Ways].
If you are using Jupyter Notebook and you need to write some text to perhaps help explain your project, you can put that text into a Markdown vell instead of a code cell. In markdown, if you want to make some text bold, surround the text with two asterisks (stars above the 8 key on the keyboard).