如何让Streamlit中的输入文本只接受字符串而不接受数字



如何让st.input_text只接受字符串?如果用户输入数字,就会弹出一条错误消息。有人有解决方案吗?

isalpha((是一个很好的选择。以下是一个只允许字符串作为输入的应用程序。

def main():
st.title("Only allow text example")

text = str(st.text_input('Type something'))
#only allow strings
if text.isalpha():
st.write(text, '...works as its a string', )
else:
st.write('Please type in a string ')

if __name__ == "__main__":
main()  

这将强制输入字符串:

user_input = st.text_input("label goes here", default_value_goes_here)
if user_input.isalpha():
st.write(text, 'string', )
else:
st.write('Please type in a string ')

您可以使用***。如果前面的字符串。是字母。

a = input("Enter input:")
if not a.isalpha():
print("Oops!  That was no valid string.  Try again...")

最新更新