Spaces:
Sleeping
Sleeping
import streamlit as st | |
# Dictionary of string methods with their explanations | |
string_methods = { | |
'replace': { | |
'syntax': "str.replace(old, new, count=-1)", | |
'description': "Replaces all occurrences of old with new. Optional argument 'count' limits the number of replacements.", | |
'example': "Example: 'hello world'.replace('world', 'Streamlit') -> 'hello Streamlit'" | |
}, | |
'capitalize': { | |
'syntax': "str.capitalize()", | |
'description': "Capitalizes the first letter of the string.", | |
'example': "Example: 'hello world'.capitalize() -> 'Hello world'" | |
}, | |
'title': { | |
'syntax': "str.title()", | |
'description': "Converts the first character of each word to uppercase.", | |
'example': "Example: 'hello world'.title() -> 'Hello World'" | |
}, | |
'upper': { | |
'syntax': "str.upper()", | |
'description': "Converts all characters to uppercase.", | |
'example': "Example: 'hello world'.upper() -> 'HELLO WORLD'" | |
}, | |
'lower': { | |
'syntax': "str.lower()", | |
'description': "Converts all characters to lowercase.", | |
'example': "Example: 'HELLO WORLD'.lower() -> 'hello world'" | |
}, | |
'find': { | |
'syntax': "str.find(sub, start=0, end=len(str))", | |
'description': "Returns the lowest index of the substring if found, otherwise returns -1.", | |
'example': "Example: 'hello world'.find('world') -> 6" | |
}, | |
'strip': { | |
'syntax': "str.strip([chars])", | |
'description': "Removes leading and trailing characters (default is whitespace).", | |
'example': "Example: ' hello world '.strip() -> 'hello world'" | |
}, | |
'partition': { | |
'syntax': "str.partition(sep)", | |
'description': "Splits the string at the first occurrence of sep into a 3-tuple: (head, sep, tail).", | |
'example': "Example: 'hello world'.partition(' ') -> ('hello', ' ', 'world')" | |
}, | |
'count': { | |
'syntax': "str.count(sub, start=0, end=len(str))", | |
'description': "Returns the number of non-overlapping occurrences of a substring.", | |
'example': "Example: 'hello world'.count('o') -> 2" | |
}, | |
'casefold': { | |
'syntax': "str.casefold()", | |
'description': "Returns a casefolded copy of the string, which is more aggressive than lower() for case-insensitive comparisons.", | |
'example': "Example: 'HELLO'.casefold() -> 'hello'" | |
}, | |
'swapcase': { | |
'syntax': "str.swapcase()", | |
'description': "Swaps case: lowercase becomes uppercase and vice versa.", | |
'example': "Example: 'Hello World'.swapcase() -> 'hELLO wORLD'" | |
}, | |
'startswith': { | |
'syntax': "str.startswith(prefix[, start[, end]])", | |
'description': "Returns True if the string starts with the specified prefix, otherwise False.", | |
'example': "Example: 'hello world'.startswith('hello') -> True" | |
}, | |
'endswith': { | |
'syntax': "str.endswith(suffix[, start[, end]])", | |
'description': "Returns True if the string ends with the specified suffix, otherwise False.", | |
'example': "Example: 'hello world'.endswith('world') -> True" | |
}, | |
'isalpha': { | |
'syntax': "str.isalpha()", | |
'description': "Returns True if all characters in the string are alphabetic and there is at least one character, otherwise False.", | |
'example': "Example: 'hello'.isalpha() -> True" | |
}, | |
'isnumeric': { | |
'syntax': "str.isnumeric()", | |
'description': "Returns True if all characters in the string are numeric characters, otherwise False.", | |
'example': "Example: '12345'.isnumeric() -> True" | |
}, | |
'isalnum': { | |
'syntax': "str.isalnum()", | |
'description': "Returns True if all characters in the string are alphanumeric and there is at least one character, otherwise False.", | |
'example': "Example: 'hello123'.isalnum() -> True" | |
}, | |
'join': { | |
'syntax': "str.join(iterable)", | |
'description': "Concatenates the strings in the iterable using the string as a separator.", | |
'example': "Example: ', '.join(['hello', 'world']) -> 'hello, world'" | |
} | |
} | |
# Streamlit app | |
st.title("Explore Python String Methods") | |
# Method selection | |
method = st.selectbox("Choose a string method to learn about:", list(string_methods.keys())) | |
# Input for user string | |
user_input = st.text_input("Enter a string to see the method in action:", "hello world") | |
# Display method details | |
if method: | |
st.write(f"### {method.capitalize()} Method") | |
st.write(f"**Syntax:** `{string_methods[method]['syntax']}`") | |
st.write(f"**Function:** {string_methods[method]['description']}") | |
st.write(f"**Example:** {string_methods[method]['example']}") | |
# Show output of the method based on user input | |
if method == 'replace': | |
old = st.text_input("Old substring:", "") | |
new = st.text_input("New substring:", "") | |
if old and new: | |
result = user_input.replace(old, new) | |
st.write(f"**Output:** `{result}`") | |
elif method == 'capitalize': | |
result = user_input.capitalize() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'title': | |
result = user_input.title() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'upper': | |
result = user_input.upper() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'lower': | |
result = user_input.lower() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'find': | |
substring = st.text_input("Substring to find:", "") | |
if substring: | |
result = user_input.find(substring) | |
st.write(f"**Output:** `{result}`") | |
elif method == 'strip': | |
chars = st.text_input("Characters to strip (leave empty for whitespace):", "") | |
result = user_input.strip(chars) if chars else user_input.strip() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'partition': | |
sep = st.text_input("Separator:", "") | |
if sep: | |
result = user_input.partition(sep) | |
st.write(f"**Output:** `{result}`") | |
elif method == 'count': | |
substring = st.text_input("Substring to count:", "") | |
if substring: | |
result = user_input.count(substring) | |
st.write(f"**Output:** `{result}`") | |
elif method == 'casefold': | |
result = user_input.casefold() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'swapcase': | |
result = user_input.swapcase() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'startswith': | |
prefix = st.text_input("Prefix to check for:", "") | |
if prefix: | |
result = user_input.startswith(prefix) | |
st.write(f"**Output:** `{result}`") | |
elif method == 'endswith': | |
suffix = st.text_input("Suffix to check for:", "") | |
if suffix: | |
result = user_input.endswith(suffix) | |
st.write(f"**Output:** `{result}`") | |
elif method == 'isalpha': | |
result = user_input.isalpha() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'isnumeric': | |
result = user_input.isnumeric() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'isalnum': | |
result = user_input.isalnum() | |
st.write(f"**Output:** `{result}`") | |
elif method == 'join': | |
iterable_input = st.text_input("Enter strings to join, separated by commas:", "hello,world") | |
if iterable_input: | |
iterable = iterable_input.split(",") | |
result = ", ".join(iterable) | |
st.write(f"**Output:** `{result}`") | |