Show API reference for

Display a single-line text input widget.

Function signature[source]

st.text_input(label, value="", max_chars=None, key=None, type="default", help=None, autocomplete=None, on_change=None, args=None, kwargs=None, *, placeholder=None, disabled=False, label_visibility="visible", icon=None, validate=None, width="stretch", bind=None, persist_state=None)

Parameters

label (str)

A short label explaining to the user what this input is for. The label can optionally contain GitHub-flavored Markdown of the following types: Bold, Italics, Strikethroughs, Inline Code, Links, and Images. Images display like icons, with a max height equal to the font height.

Unsupported Markdown elements are unwrapped so only their children (text contents) render. Common block-level Markdown (headings, lists, blockquotes) is automatically escaped and displays as literal text in labels.

See the body parameter of st.markdown for additional, supported Markdown directives.

For accessibility reasons, you should never set an empty label, but you can hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

value (object or None)

The text value of this widget when it first renders. This will be cast to str internally. If None, will initialize empty and return None until the user provides input. Defaults to empty string.

max_chars (int or None)

Max number of characters allowed in text input.

key (str, int, or None)

An optional string or integer to use as the unique key for the widget. If this is None (default), a key will be generated for the widget based on the values of the other parameters. No two widgets may have the same key. Assigning a key stabilizes the widget's identity and preserves its state across reruns even when other parameters change.

Note

Changing max_chars or the validation regex resets the widget even when a key is provided.

A key lets you read or update the widget's value via st.session_state[key]. For more details, see Widget behavior.

Additionally, if key is provided, it will be used as a CSS class name prefixed with st-key-.

type ("default", "password", "email", "url", "phone", or "search")

The type of the text input. This sets the underlying native HTML input type (which controls things like the mobile keyboard and browser autofill) and, for the specialized types, applies overridable smart defaults for icon, placeholder, validate, and autocomplete. Defaults to "default".

  • "default": A regular single-line text input. No smart defaults are applied.
  • "password": A text input that masks the user's typed value. autocomplete defaults to "new-password".
  • "email": An input for email addresses. Defaults to a mail icon, a you@example.com placeholder, email-format validation, and autocomplete="email".
  • "url": An input for web addresses. Defaults to a link icon, an https://example.com placeholder, URL-format validation, and autocomplete="url".
  • "phone": An input for phone numbers (numeric keypad on mobile). Defaults to a call icon, a +1 234 567 8900 placeholder, and autocomplete="tel". No default validation is applied because phone formats vary too widely.
  • "search": A free-text search input with a clear button that empties the field. Defaults to a search icon, a Search placeholder, and autocomplete="off" (so private search terms don't leak into the browser's autofill history). No default validation is applied.

The smart defaults are only applied when you don't pass a value for icon, placeholder, validate, or autocomplete. For each of these, None (or omission) uses the type's default, an explicit value overrides it, and "" forces the feature off (for example, icon="" shows no icon).

Note

The default email and URL validation runs in the user's browser and can be bypassed. If the validation is security-relevant, you must also validate the value on the server (in your app code) after it is submitted.

help (str or None)

A tooltip that gets displayed next to the widget label. Streamlit only displays the tooltip when label_visibility="visible". If this is None (default), no tooltip is displayed.

The tooltip can optionally contain GitHub-flavored Markdown, including the Markdown directives described in the body parameter of st.markdown.

autocomplete (str or None)

An optional value that will be passed to the <input> element's autocomplete property. If this is None (default), the value is derived from type: "new-password" for "password", "email" for "email", "url" for "url", "tel" for "phone", "off" for "search", and the empty string for "default". Pass an explicit token to override the default, or "" to fall back to the browser's default autofill behavior (equivalent to not setting the attribute). For more details, see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete

on_change (callable)

An optional callback invoked when this text input's value changes.

args (list or tuple)

An optional list or tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

placeholder (str or None)

An optional string displayed when the text input is empty. If placeholder is None (default), the placeholder is derived from type (for example, you@example.com for type="email"); for type="default" and type="password", no placeholder is displayed. Pass placeholder="" to force no placeholder even for a specialized type.

disabled (bool)

An optional boolean that disables the text input if set to True. The default is False.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. The default is "visible". If this is "hidden", Streamlit displays an empty spacer instead of the label, which can help keep the widget aligned with other widgets.

icon (str, None)

An optional emoji or icon to display within the input field to the left of the value. If icon is None (default), the icon is derived from type (for example, a mail icon for type="email"); for type="default" and type="password", no icon is displayed. Pass icon="" to force no icon even for a specialized type. If icon is a non-empty string, the following options are valid:

  • A single-character emoji. For example, you can set icon="🚨" or icon="πŸ”₯". Emoji short codes are not supported.

  • An icon from the Material Symbols library (rounded style) in the format ":material/icon_name:" where "icon_name" is the name of the icon in snake case.

    For example, icon=":material/thumb_up:" will display the Thumb Up icon. Find additional icons in the Material Symbols font library.

  • "spinner": Displays a spinner as an icon.

validate (str, tuple[str, str], or None)

An optional client-side validation rule for the input. If this is None (default), no validation is performed for type="default" and type="password", while type="email" and type="url" fall back to their built-in format validation. Pass validate="" to turn a specialized type's default validation off. If this is a string, it is treated as a JavaScript-flavored regular expression that the input must match before it can be submitted, and a generic error message is shown when validation fails. If this is a (regex, message) tuple, the regex is used for client-side validation and the custom message is shown when validation fails. Providing a custom message is recommended, since generic validation messages are less helpful to users. A user-supplied validate replaces the type's default rule.

For example, pass r"^[^@\s]+@[^@\s]+\.[^@\s]+$" to require an email-like value, or (r"^\d{3}-\d{3}-\d{4}$", "Use the format 555-123-4567.") to require a phone number and show a custom error message. Patterns are not implicitly anchored; use ^ / $ when the whole value must match (same semantics as st.column_config.TextColumn).

Validation runs when the user tries to submit a value: on blur or Enter outside a form, and on form submission inside a form. Invalid values are not submitted, and empty inputs bypass validation.

Inside a form with bind="query-params", keystrokes still stage the value into widget state (and therefore the URL) before submit-time validation runs. Form submission itself still blocks invalid values from reaching the server.

Note

This validation runs in the user's browser and can be bypassed. If the validation is security-relevant, you must also validate the value on the server (in your app code) after it is submitted.

width ("stretch" or int)

The width of the text input widget. This can be one of the following:

  • "stretch" (default): The width of the widget matches the width of the parent container.
  • An integer specifying the width in pixels: The widget has a fixed width. If the specified width is greater than the width of the parent container, the width of the widget matches the width of the parent container.

bind ("query-params" or None)

Binding mode for syncing the widget's value with a URL query parameter. If this is None (default), the widget's value is not synced to the URL. When this is set to "query-params", changes to the widget update the URL, and the widget can be initialized or updated through a query parameter in the URL. This requires key to be set. The key is used as the query parameter name.

When the widget's value equals its default, the query parameter is removed from the URL to keep it clean. A bound query parameter can't be set or deleted through st.query_params; it can only be programmatically changed through st.session_state.

This can't be used with type="password". An empty query parameter (e.g., ?my_key=) clears the widget.

persist_state ("page", "session", or None)

How long to preserve the widget's value when it isn't rendered. If this is None (default), the value is lost when the widget stops being rendered or the user switches pages. If this is "page", the value is preserved only while the user stays on the page where the widget is defined (for example, while the widget is conditionally hidden); it is discarded on a page switch and is not restored if the user returns to the page. If this is "session", the value is preserved for the entire session, including across page switches, so it returns when the user navigates back. This requires key to be set. If bind="query-params" is also set, the binding takes precedence: the value is stored in the URL, so it persists across page switches regardless of the persist_state scope. For example, st.text_input("Name", key="name", persist_state="session") keeps the entered text when the widget is hidden and shown again, or when the user navigates to another page and back.

Returns

(str or None)

The current value of the text input widget or None if no value has been provided by the user.

Examples

import streamlit as st

title = st.text_input("Movie title", "Life of Brian")
st.write("The current movie title is", title)

Use a specialized type to get a matching native input, icon, placeholder, and validation with zero extra code:

import streamlit as st

email = st.text_input("Email", type="email")
if email:
    st.write("We'll reach you at", email)

Text input widgets can customize how to hide their labels with the label_visibility parameter. If "hidden", the label doesn’t show but there is still empty space for it above the widget (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible". Text input widgets can also be disabled with the disabled parameter, and can display an optional placeholder text when the text input is empty using the placeholder parameter:

Python
import streamlit as st

# Store the initial value of widgets in session state
if "visibility" not in st.session_state:
    st.session_state.visibility = "visible"
    st.session_state.disabled = False

col1, col2 = st.columns(2)

with col1:
    st.checkbox("Disable text input widget", key="disabled")
    st.radio(
        "Set text input label visibility πŸ‘‰",
        key="visibility",
        options=["visible", "hidden", "collapsed"],
    )
    st.text_input(
        "Placeholder for the other text input widget",
        "This is a placeholder",
        key="placeholder",
    )

with col2:
    text_input = st.text_input(
        "Enter some text πŸ‘‡",
        label_visibility=st.session_state.visibility,
        disabled=st.session_state.disabled,
        placeholder=st.session_state.placeholder,
    )

    if text_input:
        st.write("You entered: ", text_input)
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.