# Slider range display string

**URL:** <https://discuss.streamlit.io/t/slider-range-display-string/33034>\
**Category:** Using Streamlit\
**Created:** [November 7, 2022, 8:03pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034 "2022-11-07T20:03:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wazarr94](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/wazarr94/32/19072_2.png) [@Wazarr94](https://discuss.streamlit.io/u/Wazarr94)\
**Post date:** [November 7, 2022, 8:03pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/1 "2022-11-07T20:03:48Z")

</div>

I wish to display a string using a translation list in a ranged slider.

For instance, in the following snippet, I have a ranged slider that goes to 1 to 3, and if the value is 1, I want the slider to display “First option”. From the docs, I see that it is easy to display “Option 1” using C style formatting, but I am not sure if what I want is possible.

Code snippet:

```nohighlight
    slider_values = [1, 2, 3]
    slider_strings = ["First option", "Second option", "Third option"]
    slider_string = st.slider(
        "String slider",
        min_value=min(slider_values),
        max_value=max(slider_values),
        value=(min(slider_values), max(slider_values)),
        format="???"
    )

```

Obviously this doesn’t work and I hope someone knows the way to do it.  
Thanks!

---

<div class="post-metadata">

**Author:** ![mathcatsand](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/mathcatsand/32/48006_2.png) [@mathcatsand](https://discuss.streamlit.io/u/mathcatsand)\
**Post date:** [November 8, 2022, 12:00am UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/2 "2022-11-08T00:00:35Z")

</div>

I don’t have a clear idea of what you are wanting to get out of the slider, so apologies if this isn’t the question.

If you want string values instead of numeric values to display, you can use `st.select_slider` instead of `slider`. You can set the initial value to a single or tuple just as before.

```auto
translate_string = ["12", "23", "34", "45", "56"]
slider_string = st.select_slider(
        "String slider",
        translate_string,
        value=("12", "56")
        )

```

---

<div class="post-metadata">

**Author:** ![Wazarr94](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/wazarr94/32/19072_2.png) [@Wazarr94](https://discuss.streamlit.io/u/Wazarr94)\
**Post date:** [November 8, 2022, 9:27am UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/3 "2022-11-08T09:27:52Z")

</div>

Sorry I wasn’t clear enough.  
I edited the initial post to add an example of what I want and why I haven’t been able to do it.

I will try to rephrase one more time to make sure this is understood better.  
Would it be possible to get the value of a ranged slider and then pass it as an index to a string list to display that string ?  
For example: slider\_string value is 2, I get the 2nd element of the slider\_strings list which is “Second option” that I then display that string instead of 2.

---

<div class="post-metadata">

**Author:** ![edsaac](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/edsaac/32/47599_2.png) [@edsaac](https://discuss.streamlit.io/u/edsaac)\
**Post date:** [November 8, 2022, 12:11pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/4 "2022-11-08T12:11:43Z")

</div>

I think `st.slider` is restricted to numeric values, `st.select_slider` should be more general in that sense, because you can pass a formatting function (see kwarg `format_func` in [st.select\_slider - Streamlit Docs](https://docs.streamlit.io/library/api-reference/widgets/st.select_slider))

![sliderFormating](https://global.discourse-cdn.com/streamlit/original/3X/9/c/9c1f92e3638bc43e2c5d7245efa16e3eb54557b8.gif)

Code:

```python
import streamlit as st

slider_values = [1,2,3]
slider_strings = ["First option", "Second option", "Third option"]

def stringify(i:int = 0) -> str:
    return slider_strings[i-1]

myslider = st.select_slider(
    "String slider",
    options=slider_values,
    value=1,
    format_func=stringify)

st.write(f"## The value of the widgets is the number, not the string: {myslider}")

```

---

<div class="post-metadata">

**Author:** ![Wazarr94](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/wazarr94/32/19072_2.png) [@Wazarr94](https://discuss.streamlit.io/u/Wazarr94)\
**Post date:** [November 8, 2022, 6:04pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/5 "2022-11-08T18:04:56Z")

</div>

Yes, I use a select slider with strings for other pages in my app it works well. I hope the Streamlit team will upgrade the ranged slider, I find the alternatives pretty mediocre.

---

<div class="post-metadata">

**Author:** ![Goyo](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/goyo/32/28219_2.png) [@Goyo](https://discuss.streamlit.io/u/Goyo)\
**Post date:** [November 8, 2022, 7:33pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/6 "2022-11-08T19:33:51Z")

</div>

So, how would the upgraded ranged slider work? Like the code in the OP, and it would return a string instead of a number?

---

<div class="post-metadata">

**Author:** ![mathcatsand](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/mathcatsand/32/48006_2.png) [@mathcatsand](https://discuss.streamlit.io/u/mathcatsand)\
**Post date:** [November 8, 2022, 7:46pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/7 "2022-11-08T19:46:45Z")

</div>

Maybe I’m still missing the point, but the `select_slider` can handle a range same as `slider` and can output numbers.

```nohighlight

values = range(5)
labels = ['first','second','third','fourth','fifth']

selection = st.select_slider('Choose a range',values,value=(1,3), format_func=(lambda x:labels[x]))

st.write(f'The selection is {selection} with values having type {type(selection[0])}.')

```

![image](https://global.discourse-cdn.com/streamlit/original/3X/d/4/d44133b21718557e1297f4f746952689c0c71028.png)

---

<div class="post-metadata">

**Author:** ![Wazarr94](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/wazarr94/32/19072_2.png) [@Wazarr94](https://discuss.streamlit.io/u/Wazarr94)\
**Post date:** [November 8, 2022, 8:04pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/8 "2022-11-08T20:04:42Z")

</div>

Whoops, you’re right, it is even written in the docs 🤦‍♂️  
Thanks, that solves the issue.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [November 8, 2023, 8:05pm UTC](https://discuss.streamlit.io/t/slider-range-display-string/33034/9 "2023-11-08T20:05:27Z")

</div>

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
