programming_language / string methods
Rakshitha2415's picture
Create string methods
43e317c verified
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': "'hello world'.replace('world', 'Streamlit') --> 'hello Streamlit'"
},
'capitalize': {
'syntax': "str.capitalize()",
'description': "Capitalizes the first letter of the string.",
'Example': "'hello world'.capitalize() --> 'Hello world'"
},
'title': {
'syntax': "str.title()",
'description': "Converts the first character of each word to uppercase.",
'Example': "'hello world'.title() --> 'Hello World'"
},
'upper': {
'syntax': "str.upper()",
'description': "Converts all characters to uppercase.",
'Example': "'hello world'.upper() --> 'HELLO WORLD'"
},
'lower': {
'syntax': "str.lower()",
'description': "Converts all characters to lowercase.",
'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': "'hello world'.find('world') --> 6"
},
'strip': {
'syntax': "str.strip([chars])",
'description': "Removes leading and trailing characters (default is whitespace).",
'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': "'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': "'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': "'HELLO'.casefold() --> 'hello'"
},
'swapcase': {
'syntax': "str.swapcase()",
'description': "Swaps case: lowercase becomes uppercase and vice versa.",
'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': "'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': "'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': "'hello'.isalpha() --> True"
},
'isnumeric': {
'syntax': "str.isnumeric()",
'description': "Returns True if all characters in the string are numeric characters, otherwise False.",
'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': "'hello123'.isalnum() --> True"
},
'join': {
'syntax': "str.join(iterable)",
'description': "Concatenates the strings in the iterable using the string as a separator.",
'Example': "', '.join(['hello', 'world']) --> 'hello, world'"
}
}
# Streamlit app
st.title("String Methods")
# Image
#about string
if st.button("Introduction To String"):
st.markdown('''
A **string** is a sequence of characters used to represent text in programming. In Python, strings are created by enclosing characters in `single ('), double ("), or triple quotes`.
Strings are `immutable`, meaning their content cannot be changed once defined, but they can be manipulated and combined in various ways using built-in methods.
**Example:** a= "Hello ALL"
''')
# 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}`")