Bocha2322/String_methods_in_python
0
1import streamlit as st2 3# Set up the main layout of the app4st.title("String Methods in Python")5 6st.write("""7### Explore various Python string methods interactively!8Enter a string below, and choose the string method you want to apply.9""")10 11# User input: Input string12user_input = st.text_input("Enter a string:", "Yes iam Batman!")13 14# User input: Choose a string method15option = st.selectbox(16 "Which string method do you want to apply?",17 ("upper", "lower", "capitalize", "title", "swapcase", "find", "replace", "split", "strip", "count")18)19# Apply string method based on user selection20if option == "upper":21 st.write(f"Result: {user_input.upper()}")22elif option == "lower":23 st.write(f"Result: {user_input.lower()}")24elif option == "capitalize":25 st.write(f"Result: {user_input.capitalize()}")26elif option == "title":27 st.write(f"Result: {user_input.title()}")28elif option == "swapcase":29 st.write(f"Result: {user_input.swapcase()}")30elif option == "find":31 sub_str = st.text_input("Enter substring to find:")32 if sub_str:33 st.write(f"Position of '{sub_str}': {user_input.find(sub_str)}")34elif option == "replace":35 old = st.text_input("Substring to replace:")36 new = st.text_input("Replacement substring:")37 if old and new:38 st.write(f"Result: {user_input.replace(old, new)}")39elif option == "split":40 delimiter = st.text_input("Enter delimiter (leave empty for spaces):", "")41 st.write(f"Result: {user_input.split(delimiter)}")42elif option == "strip":43 st.write(f"Result: {user_input.strip()}")44elif option == "count":45 sub_str = st.text_input("Enter substring to count:")46 if sub_str:47 st.write(f"Count of '{sub_str}': {user_input.count(sub_str)}")