Displaying Text
In Streamlit, displaying text is one of the most fundamental tasks. Streamlit provides several methods to display text, each suited for different purposes. Understanding these methods will help you create clear and informative web applications.
Key Concepts
1. st.write()
The st.write()
function is a versatile method in Streamlit that can display text, data, and even complex objects like charts and tables. It automatically formats the output based on the input type, making it easy to use for a variety of purposes.
2. st.text()
The st.text()
function is used to display raw text without any additional formatting. This is useful when you want to ensure that the text is displayed exactly as it is written, without any interpretation or formatting by Streamlit.
3. st.markdown()
The st.markdown()
function allows you to write text using Markdown syntax. Markdown is a lightweight markup language that allows you to format text using simple syntax. This is particularly useful for creating headings, lists, and adding links and images.
4. st.title(), st.header(), and st.subheader()
These functions are used to display text with specific formatting for titles, headers, and subheaders. They are useful for structuring your content and making it easier to read and navigate.
Examples
Example 1: Using st.write()
import streamlit as st st.write("Hello, Streamlit!") st.write("This is a simple text display.")
Example 2: Using st.text()
import streamlit as st st.text("This is raw text.") st.text("No additional formatting will be applied.")
Example 3: Using st.markdown()
import streamlit as st st.markdown("## This is a Markdown heading") st.markdown("* This is a list item") st.markdown("[Visit Streamlit](https://streamlit.io)")
Example 4: Using st.title(), st.header(), and st.subheader()
import streamlit as st st.title("Main Title") st.header("Section Header") st.subheader("Subsection Header")
By mastering these methods, you can effectively display text in your Streamlit applications, making your content clear, structured, and engaging.