import streamlit as st import io import sys # Function to display the example and explanation of a selected string method def show_string_method_example(method_name): if method_name == "Capitalize": st.subheader("Capitalize") st.markdown(""" **Description:** Capitalizes the first character of the string and lowercases all other characters. **Example:** ``` s = "hello world" s.capitalize() # "Hello world" ``` """) elif method_name == "Casefold": st.subheader("Casefold") st.markdown(""" **Description:** Converts the string to lowercase, similar to `lower()`, but more aggressive. It’s designed for case-insensitive comparisons. **Example:** ``` s = "HELLO World" s.casefold() # "hello world" ``` """) elif method_name == "Center": st.subheader("Center") st.markdown(""" **Description:** Centers the string in a field of a given width, padded with the specified character. **Example:** ``` s = "hello" s.center(10, '*') # "**hello***" ``` """) elif method_name == "Count": st.subheader("Count") st.markdown(""" **Description:** Returns the number of non-overlapping occurrences of a substring within the string. **Example:** ``` s = "hello world" s.count('o') # 2 ``` """) elif method_name == "Encode": st.subheader("Encode") st.markdown(""" **Description:** Encodes the string using the specified encoding. The default encoding is UTF-8. **Example:** ``` s = "hello" s.encode() # b'hello' ``` """) elif method_name == "Endswith": st.subheader("Endswith") st.markdown(""" **Description:** Returns True if the string ends with the specified suffix; otherwise, returns False. **Example:** ``` s = "hello world" s.endswith('world') # True ``` """) elif method_name == "Expandtabs": st.subheader("Expandtabs") st.markdown(""" **Description:** Replaces tabs in the string with spaces, using the specified tab size. **Example:** ``` s = "hello\tworld" s.expandtabs(4) # "hello world" ``` """) elif method_name == "Find": st.subheader("Find") st.markdown(""" **Description:** Returns the lowest index of the substring if it is found; otherwise, returns -1. **Example:** ``` s = "hello world" s.find('world') # 6 ``` """) elif method_name == "Format": st.subheader("Format") st.markdown(""" **Description:** Formats the string using the specified arguments. **Example:** ``` s = "Hello, {}!" s.format("Alice") # "Hello, Alice!" ``` """) elif method_name == "Index": st.subheader("Index") st.markdown(""" **Description:** Returns the lowest index of the substring if it is found; otherwise, raises a ValueError. **Example:** ``` s = "hello world" s.index('world') # 6 ``` """) elif method_name == "Isalnum": st.subheader("Isalnum") st.markdown(""" **Description:** Returns True if all characters in the string are alphanumeric (letters and numbers) and there is at least one character; otherwise, returns False. **Example:** ``` s = "hello123" s.isalnum() # True ``` """) elif method_name == "Isalpha": st.subheader("Isalpha") st.markdown(""" **Description:** Returns True if all characters in the string are alphabetic and there is at least one character; otherwise, returns False. **Example:** ``` s = "hello" s.isalpha() # True ``` """) elif method_name == "Isdecimal": st.subheader("Isdecimal") st.markdown(""" **Description:** Returns True if all characters in the string are decimal characters and there is at least one character; otherwise, returns False. **Example:** ``` s = "123" s.isdecimal() # True ``` """) elif method_name == "Isdigit": st.subheader("Isdigit") st.markdown(""" **Description:** Returns True if all characters in the string are digits and there is at least one character; otherwise, returns False. **Example:** ``` s = "123" s.isdigit() # True ``` """) elif method_name == "Isidentifier": st.subheader("Isidentifier") st.markdown(""" **Description:** Returns True if the string is a valid identifier (e.g., variable name); otherwise, returns False. **Example:** ``` s = "hello_world" s.isidentifier() # True ``` """) elif method_name == "Islower": st.subheader("Islower") st.markdown(""" **Description:** Returns True if all characters in the string are lowercase and there is at least one character; otherwise, returns False. **Example:** ``` s = "hello" s.islower() # True ``` """) elif method_name == "Isnumeric": st.subheader("Isnumeric") st.markdown(""" **Description:** Returns True if all characters in the string are numeric and there is at least one character; otherwise, returns False. **Example:** ``` s = "123" s.isnumeric() # True ``` """) elif method_name == "Isprintable": st.subheader("Isprintable") st.markdown(""" **Description:** Returns True if all characters in the string are printable or the string is empty; otherwise, returns False. **Example:** ``` s = "hello" s.isprintable() # True ``` """) elif method_name == "Isspace": st.subheader("Isspace") st.markdown(""" **Description:** Returns True if all characters in the string are whitespace and there is at least one character; otherwise, returns False. **Example:** ``` s = " " s.isspace() # True ``` """) elif method_name == "Istitle": st.subheader("Istitle") st.markdown(""" **Description:** Returns True if the string is in title case (i.e., each word starts with an uppercase letter and the rest are lowercase); otherwise, returns False. **Example:** ``` s = "Hello World" s.istitle() # True ``` """) elif method_name == "Isupper": st.subheader("Isupper") st.markdown(""" **Description:** Returns True if all characters in the string are uppercase and there is at least one character; otherwise, returns False. **Example:** ``` s = "HELLO" s.isupper() # True ``` """) elif method_name == "Join": st.subheader("Join") st.markdown(""" **Description:** Concatenates the elements of an iterable (like a list) into a single string, with the string as the separator. **Example:** ``` s = " " s.join(["Hello", "World"]) # "Hello World" ``` """) elif method_name == "Ljust": st.subheader("Ljust") st.markdown(""" **Description:** Left-justifies the string in a field of a given width, padded with the specified character. **Example:** ``` s = "hello" s.ljust(10, '*') # "hello*****" ``` """) elif method_name == "Lower": st.subheader("Lower") st.markdown(""" **Description:** Converts all characters in the string to lowercase. **Example:** ``` s = "HELLO" s.lower() # "hello" ``` """) elif method_name == "Lstrip": st.subheader("Lstrip") st.markdown(""" **Description:** Removes leading characters specified in chars from the string. **Example:** ``` s = " hello" s.lstrip() # "hello" ``` """) elif method_name == "Rfind": st.subheader("Rfind") st.markdown(""" **Description:** Returns the highest index of the substring if it is found; otherwise, returns -1. **Example:** ``` s = "hello world" s.rfind('o') # 7 ``` """) elif method_name == "Rindex": st.subheader("Rindex") st.markdown(""" **Description:** Returns the highest index of the substring if it is found; otherwise, raises a ValueError. **Example:** ``` s = "hello world" s.rindex('o') # 7 ``` """) elif method_name == "Rjust": st.subheader("Rjust") st.markdown(""" **Description:** Right-justifies the string in a field of a given width, padded with the specified character. **Example:** ``` s = "hello" s.rjust(10, '*') # "*****hello" ``` """) elif method_name == "Rpartition": st.subheader("Rpartition") st.markdown(""" **Description:** Splits the string into a tuple of three elements: the part before the separator, the separator itself, and the part after the separator. Searches for the separator from the end of the string. **Example:** ``` s = "hello,world,again" s.rpartition(',') # ('hello,world', ',', 'again') ``` """) elif method_name == "Rsplit": st.subheader("Rsplit") st.markdown(""" **Description:** Splits the string from the end, using the specified separator and returning a list of the words. If maxsplit is specified, it limits the number of splits. **Example:** ``` s = "a,b,c,d" s.rsplit(',', 2) # ['a,b', 'c', 'd'] ``` """) elif method_name == "Rstrip": st.subheader("Rstrip") st.markdown(""" **Description:** Removes trailing characters specified in chars from the string. **Example:** ``` s = "hello " s.rstrip() # "hello" ``` """) elif method_name == "Split": st.subheader("Split") st.markdown(""" **Description:** Splits the string using the specified separator and returns a list of the words. If maxsplit is specified, it limits the number of splits. **Example:** ``` s = "a,b,c,d" s.split(',') # ['a', 'b', 'c', 'd'] ``` """) elif method_name == "Splitlines": st.subheader("Splitlines") st.markdown(""" **Description:** Splits the string at line breaks and returns a list of the lines. **Example:** ``` s = "hello\nworld" s.splitlines() # ['hello', 'world'] ``` """) elif method_name == "Startswith": st.subheader("Startswith") st.markdown(""" **Description:** Returns True if the string starts with the specified prefix; otherwise, returns False. **Example:** ``` s = "hello world" s.startswith('hello') # True ``` """) elif method_name == "Strip": st.subheader("Strip") st.markdown(""" **Description:** Removes leading and trailing characters specified in chars from the string. **Example:** ``` s = " hello " s.strip() # "hello" ``` """) elif method_name == "Swapcase": st.subheader("Swapcase") st.markdown(""" **Description:** Converts uppercase characters to lowercase and lowercase characters to uppercase. **Example:** ``` s = "Hello World" s.swapcase() # "hELLO wORLD" ``` """) elif method_name == "Title": st.subheader("Title") st.markdown(""" **Description:** Converts the first character of each word to uppercase and the rest to lowercase. **Example:** ``` s = "hello world" s.title() # "Hello World" ``` """) elif method_name == "Upper": st.subheader("Upper") st.markdown(""" **Description:** Converts all characters in the string to uppercase. **Example:** ``` s = "hello" s.upper() # "HELLO" ``` """) elif method_name == "Zfill": st.subheader("Zfill") st.markdown(""" **Description:** Pads the string with zeros on the left, to fill the specified width. **Example:** ``` s = "42" s.zfill(5) # "00042" ``` """) else: st.write("Please select a valid string method.") # List of string methods methods = [ "Capitalize", "Casefold", "Center", "Count", "Encode", "Endswith", "Expandtabs", "Find", "Format", "Index", "Isalnum", "Isalpha", "Isdecimal", "Isdigit", "Isidentifier", "Islower", "Isnumeric", "Isprintable", "Isspace", "Istitle", "Isupper", "Join", "Ljust", "Lower", "Lstrip", "Partition", "Replace", "Rfind", "Rindex", "Rjust", "Rpartition", "Rsplit", "Rstrip", "Split", "Splitlines", "Startswith", "Strip", "Swapcase", "Title", "Upper", "Zfill" ] def execute_user_code(): st.subheader("Try it Yourself") code_input = st.text_area("Write your Python code here:", height=200) if st.button("Run Code"): try: # Capture the output original_stdout = sys.stdout # Save a reference to the original standard output result = io.StringIO() # Create a string buffer to capture the output sys.stdout = result # Redirect standard output to the string buffer exec(code_input) # Execute the user-provided code sys.stdout = original_stdout # Reset the standard output to its original value st.text("Output:") st.code(result.getvalue()) # Display the captured output except Exception as e: st.error(f"An error occurred: {e}") # Streamlit app layout st.title("Python String Methods Explorer") selected_method = st.selectbox("Select a String Method", methods) show_string_method_example(selected_method) # In your main Streamlit app code execute_user_code()