Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Text, Data and Charts

Text Elements

Display text in Streamlit with title, header, subheader, markdown, caption, code, latex and divider. Learn colored text, badges, emoji and how to format numbers for display.

Every app starts with words: a title, headings, explanations, captions under a chart. Streamlit has a small family of text functions, each with a different visual weight. Choosing the right one makes your app scannable, and it also builds the heading structure that screen readers and search engines understand.

The Streamlit code in this lesson is real and read-only. Each block says what the page would show. The runnable blocks are stand-ins and plain-Python helpers for formatting values before you display them.

The heading family

Python
import streamlit as st

st.title("Coffee Shop Dashboard")
st.header("Sales this week")
st.subheader("By drink type")
st.write("Regular body text goes here.")
st.caption("Source: point-of-sale export, updated hourly.")

The page shows a very large title, a smaller header, a smaller subheader, normal paragraph text and finally small grey caption text. They are ranked, like HTML h1, h2, h3. Use one st.title per page, then headers and subheaders for sections. st.header and st.subheader also accept divider="rainbow" (or another color) to draw a colored line below the heading, and help="..." to attach a small question-mark tooltip.

Function Use for
st.title The page name, once
st.header Main sections
st.subheader Parts of a section
st.markdown Rich paragraphs, lists, links, tables
st.write Quick display of anything (see Magic and st.write)
st.caption Small notes, sources, units
st.text Fixed-width plain text, no Markdown
st.code Source code with syntax highlighting
st.latex Math formulas
st.divider A horizontal line between parts

st.markdown: rich text

st.markdown renders a Markdown string. It supports bold, italics, lists, links, tables and inline code, plus a few Streamlit extras:

Python
import streamlit as st

st.markdown("""
## Release notes
- **Faster** charts
- *New* export button
- See the [docs](https://docs.streamlit.io)

| Plan | Price |
|------|-------|
| Free | $0 |
| Pro  | $12 |
""")

st.markdown("Status: :green[online] and :red[2 alerts]")
st.markdown("Tags: :blue-badge[python] :orange-badge[data]")
st.markdown("Press :material/search: to search, or :balloon: for a surprise")

The first call shows a heading, a bullet list with a working link, and a two-column table. The second prints "online" in green and "2 alerts" in red. The third shows small colored badge pills. The fourth shows a Material icon and an emoji inline. The color names are blue, green, orange, red, violet, gray, grey, rainbow and primary.

A triple-quoted string keeps line breaks, but indented lines would be treated as a code block, so start the text at the left edge as above (or use textwrap.dedent).

By default raw HTML is escaped for safety. unsafe_allow_html=True lets you inject HTML, but then you are responsible for it. Never pass user-provided text into it, or visitors could inject scripts. Prefer Markdown and the layout functions in the layout chapter.

st.text, st.code and st.latex

Python
import streamlit as st

st.text("Fixed width.   Spaces   stay   as   typed.")

st.code("""
def greet(name):
    return f"Hello, {name}!"
""", language="python", line_numbers=True)

st.latex(r"E = mc^2")
st.latex(r"\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i")

st.text shows monospace text with no formatting. st.code shows a highlighted box with a copy button; set language to "python", "sql", "bash", "json" and so on. The line_numbers=True option adds a gutter. st.latex typesets formulas; write the formula in a raw string (r"...") so Python does not eat the backslashes. You can also write LaTeX inside Markdown between dollar signs: st.markdown("Area is $\\pi r^2$").

st.code is for showing code. To show the code of the app itself next to its output, use with st.echo(): from the previous lesson.

Emoji and icons

Emoji work anywhere in text: st.title("Weather โ˜€๏ธ"). Many functions (st.button, st.page_config, tabs, expanders) also take an icon= argument. Streamlit supports Material Symbols in the form ":material/home:", which keeps icons crisp and consistent. Do not use emoji or icons as the only carrier of meaning: a screen reader reads "red circle" or nothing at all, so keep a word with it.

Slides: the text family at a glance

Pick text by role

title Once per page
header Main sections
markdown Rich paragraphs
caption Small notes
1 / 2

Format values before you display them

Text elements display strings, so the numbers you show should be formatted well: thousands separators, fixed decimals, percentages and currency. Do this in plain Python with f-strings. The logic below is what you would run just before st.write or st.metric.

Python Format numbers for display

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A compact helper like this is handy for big numbers on metric cards, where a long number would be cut off. The same f-string formats work in every Streamlit call that takes text.

Building a Markdown table

Markdown tables are good for small, static summaries because they render with the page text. For data that should sort or scroll, use st.dataframe instead (next lesson). To produce a Markdown table from rows, build the string yourself. This runnable block creates the text you would pass to st.markdown, and a stand-in prints how heading levels nest.

Python Build Markdown text from data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a real app you would call st.markdown(table_md); the page shows a three-column table with right-aligned numeric columns.

Common mistakes

  • Several titles. Use st.title once; use headers below it.
  • Giant paragraphs in st.write. Split text into short paragraphs and lists; readers scan.
  • Using color alone. :red[late] is fine, but keep the word so color-blind users get the message.
  • Forgetting the raw string for LaTeX. "\frac" contains a form-feed escape; use r"\frac".
  • Enabling unsafe_allow_html with user text. It is an injection risk.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function should appear once per page as the main name?

    st.title is the top-level heading; use it once and organize sections with header and subheader.
  2. 2. How do you color a word green inside Markdown text?

    Streamlit Markdown supports color directives like :green[text] and :red[text].
  3. 3. Why use r"..." with st.latex?

    LaTeX uses backslashes (\frac, \sum). A raw string stops Python from treating them as escape sequences.
  4. 4. Which element shows small grey explanatory text such as a data source?

    st.caption is meant for small notes under charts and tables.

Summary

  • title, header, subheader, caption and markdown give text a clear hierarchy.
  • Markdown supports lists, tables, links, color directives, badges and icons; avoid unsafe_allow_html with untrusted text.
  • st.code highlights source, st.latex shows formulas, st.text shows monospaced plain text.
  • Format numbers with f-strings before displaying them.

Next: DataFrames and Tables