How do I get the full text from text_input with for loop?

Hello , I have tried text_input and text_area and saving them into a session state.

However , when i try to pull our from that session state in for loop method.
it always put a single string for me.

for exaple: when i enter “apple” in text_input.

and i get 「a,p,p,l,e」

is there any solution? thanks


Hi @Theo_K

Could you clarify or provide example of the intended output that you’d like to see. A diagram would be helpful.

1 Like

You’re looping through a string, which will give you one character at a time – you don’t need a loop, just get the item and print it. Here are two ways to accomplish that:

import streamlit as st

text = st.text_input("Text input", key="text_input")

if st.button("run"):
    st.write(st.session_state["text_input"])
    # OR
    st.write(text)
2 Likes

Hello, thanks for your answer, if i want to put more than 2 string in a ‘session_state’ , and iterate the session_state, is there any way to get that? thanks!!

For example

st.session_state[‘key’] = ‘apple’ , ‘grape’

for i in st.session_state[‘key’]:
print(i)


I want to get ‘apple’, ‘grape’, not seperated string such as ‘a’,‘p’,‘p’,‘l’,‘e’

st.session_state['key'] = [‘apple’ , ‘grape’]

for i in st.session_state['key']:
    print(i)

If it’s a list, that will work just fine

If it’s just a giant string, separated with commas:

st.session_state['key'] = ‘apple, grape’

for i in st.session_state['key'].split(', '):
    print(i)
2 Likes

Thanks! the best answer !!!

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