This guide will explain how to use markdown syntax in Jupyter Notebook.
Using Markdown syntax in Jupyter Notebook
Jupyter notebook supports the markdown syntax. Jupyter Notebook consists of cells to organize the notebook content. It contains both cells that render text written using the Markdown syntax and cells that contain programming code. The use of markdown syntax in the Jupyter notebook allows you to add both the plain text, notations, and code in one document so that you can easily manage your workflows in a document.
Markdown is a markup language, superset of HTML, allowing you to write the HTML language in a shortened way. Below we have mentioned how to add content in a document using a markdown language:
You need a Jupyter Notebook environment to add the markdown syntax in a file. Open a new Jupyter notebook. The following interface will show in the Jupyter notebook:
By default, the code cell is selected in the jupyter notebook.
You can add the markdown cells in the jupyter notebook from the drop-down list as follows:
You can also add the markdown option by using the keyboard shortcut ‘m/M’ immediately after adding a new cell.
Markdown syntax basics
Below, we will demonstrate some markdown markup tags basics that are usually used in markdown text formatting.
Adding Headings using Markdown
To add heading1 or first heading in the markdown syntax, use the hash ‘#’ symbol followed by the space. Similarly, for heading2, heading3 etc., add more # symbols at the start of each heading. Below, we have shown some examples for adding headings in the markdown (.md) file:
## Heading2
### Heading3
To execute the above tags, press ‘Shift+Enter’ or click on the ‘Run’ option.
Format Text (Bold, Italic)
To format text as bold, surrounding the text block use double ‘*’ symbols, and for Italic, use single ‘*’ symbol as follows:
You can also bold and italic text using the following symbols:
Add Block quotes
Generally, we use block quotes in a document to add some important notes or instructions. Block quotes contain large text chunks in indented format. To add a block quote in the markdown file, use the ‘>’ symbol at the start of the text as follows:
Add Ordered and Unordered List Items
To add the Ordered bullet list in a markdown file, use the numbers at the start of each list item or use <ol> tag ending with </ol> that contains the list items between <li> and </li> tags as follows:
To add an unordered bullet list in a markdown file, use the ‘-’ or ‘*’ symbol followed by the space as follows:
Alternatively, you can also use the markup tags <ul> ending with </ul> contains list items between <li> and </li> tags.
Add Horizontal Line
Use the three ‘- – -’ hyphens to add a horizontal line by using the markdown. Or you can also use the <hr> tag to add a horizontal line.
Add Code Blocks
You can also insert code section or inline code using the markdown in the Jupyter notebook. To add the inline code, use the back ticks surrounding the code, such as `var a=8`.
To add the code block section using the markdown, insert the three back ticks (“`) at the start of the block code as follows:
str = "This is an example of block code"
print(str)
```
Add Tables
The table contains the details in the form of rows and columns. You can also add a table by combining the vertical pipe ‘|’ symbol and the hyphen symbol to create the header. Using the following markdown syntax, you can easily add a table in the .md file:
|--- |----------| ------|
| 01 | Samreena | 200 |
| 02 | Asif | 300 |
| 03 | Mirha | 150 |
Add external links and Images
To add a link to another page or website, place the hyperlink text in the square brackets followed by the URL in the parentheses as follows:
[Link Text](https://linuxhint.com/generate-ssh-key-ubuntu/ " Title (Optional)")
You can also add images in the Jupyter notebook using the markdown. Select markdown cells from the top menu and click on the Edit -> insert image.
Now, choose the image from your system as follows:
You can also add images in a similar way to external text links. To add an image in a markdown (.md) extension, use the exclamation mark (!) at the start then, followed by Alt text in brackets, and insert the image URL or path in parentheses as follows:
![my-sample-image](img/mysmapleimage.jpg)
Conclusion
We have demonstrated how to use format text using the markdown in Jupyter Notebook. Using the HTML tags, you can also format plain text in markdown. Using above all guidelines, you can create a professional markdown (.md) file in Jupyter Notebook. In Jupyter notebook, you can easily add programming code inside plain text documents using the markdown format.