CoolFace
Apppublic

Rakshitha2415/pyhton_programming

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py185 linesDownload Raw Back to root
1import streamlit as st2 3# Dictionary of string methods with their explanations4string_methods = {5    'replace': {6        'syntax': "str.replace(old, new, count=-1)",7        'description': "Replaces all occurrences of old with new. Optional argument 'count' limits the number of replacements.",8        'Example': "'hello world'.replace('world', 'Streamlit') --> 'hello Streamlit'"9    },10    'capitalize': {11        'syntax': "str.capitalize()",12        'description': "Capitalizes the first letter of the string.",13        'Example': "'hello world'.capitalize() --> 'Hello world'"14    },15    'title': {16        'syntax': "str.title()",17        'description': "Converts the first character of each word to uppercase.",18        'Example': "'hello world'.title() --> 'Hello World'"19    },20    'upper': {21        'syntax': "str.upper()",22        'description': "Converts all characters to uppercase.",23        'Example': "'hello world'.upper() --> 'HELLO WORLD'"24    },25    'lower': {26        'syntax': "str.lower()",27        'description': "Converts all characters to lowercase.",28        'Example': "'HELLO WORLD'.lower() --> 'hello world'"29    },30    'find': {31        'syntax': "str.find(sub, start=0, end=len(str))",32        'description': "Returns the lowest index of the substring if found, otherwise returns -1.",33        'Example': "'hello world'.find('world') --> 6"34    },35    'strip': {36        'syntax': "str.strip([chars])",37        'description': "Removes leading and trailing characters (default is whitespace).",38        'Example': "' hello world '.strip() --> 'hello world'"39    },40    'partition': {41        'syntax': "str.partition(sep)",42        'description': "Splits the string at the first occurrence of sep into a 3-tuple: (head, sep, tail).",43        'Example': "'hello world'.partition(' ') --> ('hello', ' ', 'world')"44    },45    'count': {46        'syntax': "str.count(sub, start=0, end=len(str))",47        'description': "Returns the number of non-overlapping occurrences of a substring.",48        'Example': "'hello world'.count('o') --> 2"49    },50    'casefold': {51        'syntax': "str.casefold()",52        'description': "Returns a casefolded copy of the string, which is more aggressive than lower() for case-insensitive comparisons.",53        'Example': "'HELLO'.casefold() --> 'hello'"54    },55    'swapcase': {56        'syntax': "str.swapcase()",57        'description': "Swaps case: lowercase becomes uppercase and vice versa.",58        'Example': "'Hello World'.swapcase() --> 'hELLO wORLD'"59    },60    'startswith': {61        'syntax': "str.startswith(prefix[, start[, end]])",62        'description': "Returns True if the string starts with the specified prefix, otherwise False.",63        'Example': "'hello world'.startswith('hello') --> True"64    },65    'endswith': {66        'syntax': "str.endswith(suffix[, start[, end]])",67        'description': "Returns True if the string ends with the specified suffix, otherwise False.",68        'Example': "'hello world'.endswith('world') --> True"69    },70    'isalpha': {71        'syntax': "str.isalpha()",72        'description': "Returns True if all characters in the string are alphabetic and there is at least one character, otherwise False.",73        'Example': "'hello'.isalpha() --> True"74    },75    'isnumeric': {76        'syntax': "str.isnumeric()",77        'description': "Returns True if all characters in the string are numeric characters, otherwise False.",78        'Example': "'12345'.isnumeric() --> True"79    },80    'isalnum': {81        'syntax': "str.isalnum()",82        'description': "Returns True if all characters in the string are alphanumeric and there is at least one character, otherwise False.",83        'Example': "'hello123'.isalnum() --> True"84    },85    'join': {86        'syntax': "str.join(iterable)",87        'description': "Concatenates the strings in the iterable using the string as a separator.",88        'Example': "', '.join(['hello', 'world']) --> 'hello, world'"89    }90}91 92# Streamlit app93st.title("String Methods")94# Image95#about string96if st.button("Introduction To String"):97    st.markdown('''98        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`. 99        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.100        **Example:** a= "Hello ALL"101        ''')102 103# Method selection104method = st.selectbox("Choose a string method to learn about:", list(string_methods.keys()))105 106# Input for user string107user_input = st.text_input("Enter a string to see the method in action:", "hello world")108 109# Display method details110if method:111    st.write(f"### {method.capitalize()} Method")112    st.write(f"**Syntax:** `{string_methods[method]['syntax']}`")113    st.write(f"**Function:** {string_methods[method]['description']}")114    st.write(f"**Example:** {string_methods[method]['Example']}")115    116    # Show output of the method based on user input117    if method == 'replace':118        old = st.text_input("Old substring:", "")119        new = st.text_input("New substring:", "")120        if old and new:121            result = user_input.replace(old, new)122            st.write(f"**Output:** `{result}`")123    elif method == 'capitalize':124        result = user_input.capitalize()125        st.write(f"**Output:** `{result}`")126    elif method == 'title':127        result = user_input.title()128        st.write(f"**Output:** `{result}`")129    elif method == 'upper':130        result = user_input.upper()131        st.write(f"**Output:** `{result}`")132    elif method == 'lower':133        result = user_input.lower()134        st.write(f"**Output:** `{result}`")135    elif method == 'find':136        substring = st.text_input("Substring to find:", "")137        if substring:138            result = user_input.find(substring)139            st.write(f"**Output:** `{result}`")140    elif method == 'strip':141        chars = st.text_input("Characters to strip (leave empty for whitespace):", "")142        result = user_input.strip(chars) if chars else user_input.strip()143        st.write(f"**Output:** `{result}`")144    elif method == 'partition':145        sep = st.text_input("Separator:", "")146        if sep:147            result = user_input.partition(sep)148            st.write(f"**Output:** `{result}`")149    elif method == 'count':150        substring = st.text_input("Substring to count:", "")151        if substring:152            result = user_input.count(substring)153            st.write(f"**Output:** `{result}`")154    elif method == 'casefold':155        result = user_input.casefold()156        st.write(f"**Output:** `{result}`")157    elif method == 'swapcase':158        result = user_input.swapcase()159        st.write(f"**Output:** `{result}`")160    elif method == 'startswith':161        prefix = st.text_input("Prefix to check for:", "")162        if prefix:163            result = user_input.startswith(prefix)164            st.write(f"**Output:** `{result}`")165    elif method == 'endswith':166        suffix = st.text_input("Suffix to check for:", "")167        if suffix:168            result = user_input.endswith(suffix)169            st.write(f"**Output:** `{result}`")170    elif method == 'isalpha':171        result = user_input.isalpha()172        st.write(f"**Output:** `{result}`")173    elif method == 'isnumeric':174        result = user_input.isnumeric()175        st.write(f"**Output:** `{result}`")176    elif method == 'isalnum':177        result = user_input.isalnum()178        st.write(f"**Output:** `{result}`")179    elif method == 'join':180        iterable_input = st.text_input("Enter strings to join, separated by commas:", "hello,world")181        if iterable_input:182            iterable = iterable_input.split(",")183            result = ", ".join(iterable)184            st.write(f"**Output:** `{result}`")185