CoolFace
Apppublic

ursswag/StringMethods

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py368 linesDownload Raw Back to root
1import streamlit as st2st.markdown("<h1 style='color:blue;'>String Master</h1>", unsafe_allow_html=True)3if st.button('Fundamentals of Strings'):4    st.markdown(''' **Strings**  in python are surrounded by either single quotation marks, or double quotation marks.5        'hello' is the same as "hello".6        You can display a string literal with the 'print()' function:7    ''')8    st.markdown('''You can use quotes inside a string, as long as they don't match the quotes surrounding the string9    Assigning a string to a variable is done with the variable name followed by an equal sign and the string:''')10    example_code = '''11    # Example Code12a="It's Alright"13b="He is called 'Johnny'"14c='He is called "Johnny"'15print(a)16print(b)17print(c)18 19        20        '''21        22    st.code(example_code, language='python')23        24    a="It's alright"25    b="He is called 'Johnny'"26    c='He is called "Johnny"'27        28    st.write("Example Output:",a,b,c)29st.image("WhatsApp Image 2024-09-10 at 17.00.02_90a530ed.jpg")30method = st.selectbox("choose a string method",["Uppercase","Lowercase","Swapcase","Split","Splitlines","Strip","Count","Zfill","Startswith","Endswith","partition","join","isalpha","isnumeric","isalnum","expandtabs","center","replace","find","casefold"])31 32if st.button(f'click here to learn about {method}'):33    if method == "Uppercase":34        st.markdown("<h1 style='color:blue;'>Uppercase()</h1>", unsafe_allow_html=True)35        st.markdown('''36        ## uppercase() Method37        The upper() method returns a string where all characters are in upper case.38        Symbols and Numbers are ignored.39        ### Syntax40        'string.upper()'41        42        43        44        45        46        ''')47        ## **Example Code**48        example_code = '''49        # Example Code50        txt = "Hello my friends"51        x = txt.upper()52        print(x)53        '''54        55        st.code(example_code, language='python')56        57        58        txt = "Hello my friends"59        x = txt.upper()60        st.write("Example Output:", x)61    elif method == "Lowercase()":62        st.markdown("<h1 style='color:blue;'>Lowercase()</h1>", unsafe_allow_html=True)63        st.markdown('''64        ## Lowercase() Method65        The lower() method returns a string where all characters are lower case.Symbols and Numbers are ignored.66        ### Syntax67        'string.lower()'68        69        70        71        72        73        ''')74        ## **Example Code**75        example_code = '''76        # Example Code77        txt = "Hello my FRIENDS"78 79        x = txt.lower()80 81        print(x)82 83        '''84        85        st.code(example_code, language='python')86        87        88        txt = "Hello my FRIENDS"89 90        x = txt.lower()91 92        st.write("Example output:", x)93    elif method=='Swapcase':94        st.markdown("<h1 style='color:blue;'>Swapcase()</h1>", unsafe_allow_html=True)95        st.markdown('''96        ## Swapcase() Method97        98        The swapcase() method returns a string where all the upper case letters are lower case and vice versa.99        ### Syntax100        'string.swapcase()'101        ''')102        ## **Example Code**103        example_code='''104        # Example Code105        txt = "Hello My Name Is PETER"106 107        x = txt.swapcase()108 109        print(x)110 111        112        '''113        st.code(example_code, language='python')114        txt = "Hello My Name Is PETER"115        x = txt.swapcase()116        st.write("example output:", x)117    elif method == 'Split':118        st.markdown("<h1 style='color:blue;'>Split()</h1>", unsafe_allow_html=True)119        st.markdown('''120    ## Split() Method121    122    The split() method splits a string into a list.123    ### Syntax124    'string.split(separator, maxsplit)'125    ''')126    ## **Example Code**127        example_code = '''128    # Example Code129    txt = "Hello, my friends"130    x = txt.split(", ")131    print(x)132    '''133        st.code(example_code, language='python')134        txt = "Hello, my friends"135        x = txt.split(", ")136        st.write("Example Output:", x)137    elif method == 'Splitlines':138        st.markdown("<h1 style='color:blue;'>Splitlines()</h1>", unsafe_allow_html=True)139        st.markdown('''140    ## Splitlines() Method141    142    The splitlines() method splits a string into a list at line breaks.143    ### Syntax144    'string.splitlines(keepends)'145    ''')146    ## **Example Code**147        example_code = '''148    # Example Code149    txt = "Hello\\nmy friends"150    x = txt.splitlines()151    print(x)152    '''153        st.code(example_code, language='python')154        txt = "Hello\nmy friends"155        x = txt.splitlines()156        st.write("Example Output:", x)157    elif method == 'Strip':158        st.markdown("<h1 style='color:blue;'>Strip()</h1>", unsafe_allow_html=True)159        st.markdown('''160    ## Strip() Method161    162    The Strip() method removes any leading and trailing characters (space is the default).163    ### Syntax164    'string.strip(chars)'165    ''')166    ## **Example Code**167        example_code = '''168    # Example Code169    txt = "   Hello my friends   "170    x = txt.strip()171    print(x)172    '''173        st.code(example_code, language='python')174        txt = "   Hello my friends   "175        x = txt.strip()176        st.write("Example Output:", x)177    elif method == 'Count':178        st.markdown("<h1 style='color:blue;'>Count()</h1>", unsafe_allow_html=True)179        st.markdown('''180    ## Count() Method181    182    The count() method returns the number of times a specified value appears in the string.183    ### Syntax184    'string.count(value, start, end)'185    ''')186    ## **Example Code**187        example_code = '''188    # Example Code189    txt = "Hello my friends"190    x = txt.count("e")191    print(x)192    '''193        st.code(example_code, language='python')194        txt = "Hello my friends"195        x = txt.count("e")196        st.write("Example Output:", x)197    elif method == 'Zfill':198        st.markdown("<h1 style='color:blue;'Zfill()</h1>", unsafe_allow_html=True)199        st.markdown('''200    ## Zfill() Method201    202    The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length.203    ### Syntax204    'string.zfill(length)'205    ''')206    ## **Example Code**207        example_code = '''208    # Example Code209    txt = "42"210    x = txt.zfill(5)211    print(x)212    '''213        st.code(example_code, language='python')214        txt = "42"215        x = txt.zfill(5)216        st.write("Example Output:", x)217    elif method == 'Startswith':218        st.markdown("<h1 style='color:blue;'>Startswith()</h1>", unsafe_allow_html=True)219        st.markdown('''220    ## Startswith() Method221    222    The startswith() method returns True if the string starts with the specified value, otherwise False.223    ### Syntax224    'string.startswith(value, start, end)'225    ''')226    ## **Example Code**227        example_code = '''228    # Example Code229    txt = "Hello my friends"230    x = txt.startswith("Hello")231    print(x)232    '''233        st.code(example_code, language='python')234        txt = "Hello my friends"235        x = txt.startswith("Hello")236        st.write("Example Output:", x)237    238    elif method == 'Endswith':239        st.markdown("<h1 style='color:blue;'>Endswith()</h1>", unsafe_allow_html=True)240        st.markdown('''241    ## Endswith() Method242    243    The endswith() method returns True if the string ends with the specified value, otherwise False.244    ### Syntax245    'string.endswith(value, start, end)'246    ''')247    ## **Example Code**248        example_code = '''249    # Example Code250    txt = "Hello my friends"251    x = txt.endswith("friends")252    print(x)253    '''254        st.code(example_code, language='python')255        txt = "Hello my friends"256        x = txt.endswith("friends")257        st.write("Example Output:", x)258    elif method == 'partition':259        st.markdown("<h1 style='color:blue;'>Partition()</h1>", unsafe_allow_html=True)260        st.markdown('''261    ## partition() Method262    263    The partition() method searches for a specified string and splits the string into a tuple containing three elements.264    ### Syntax265    'string.partition(value)'266    ''')267    ## **Example Code**268        example_code = '''269    # Example Code270    txt = "I could eat bananas all day"271    x = txt.partition("bananas")272    print(x)273    '''274        st.code(example_code, language='python')275        txt = "I could eat bananas all day"276        x = txt.partition("bananas")277        st.write("Example Output:", x)278    elif method == 'join':279        st.markdown("<h1 style='color:blue;'>Join()</h1>", unsafe_allow_html=True)280        st.markdown('''281    ## Join() Method282    283    The join() method takes all items in an iterable and joins them into one string.284    ### Syntax285    'separator.join(iterable)'286    ''')287    ## **Example Code**288        example_code = '''289    # Example Code290    my_list = ["John", "Peter", "Vicky"]291    x = ", ".join(my_list)292    print(x)293    '''294        st.code(example_code, language='python')295        my_list = ["John", "Peter", "Vicky"]296        x = ", ".join(my_list)297        st.write("Example Output:", x)298    elif method == 'find':299        st.markdown("<h1 style='color:blue;'>Find()</h1>", unsafe_allow_html=True)300        st.markdown('''301    ## find() Method302    303    The find() method finds the first occurrence of the specified value.304    ### Syntax305    'string.find(value, start, end)'306    ''')307    ## **Example Code**308        example_code = '''309    # Example Code310    txt = "Hello, welcome to my world."311    x = txt.find("welcome")312    print(x)313    '''314        st.code(example_code, language='python')315        txt = "Hello, welcome to my world."316        x = txt.find("welcome")317        st.write("Example Output:", x)318 319    elif method == 'find':320        st.markdown('''321    ## find() Method322    323    The find() method finds the first occurrence of the specified value.324    ### Syntax325    'string.find(value, start, end)'326    ''')327    ## **Example Code**328        example_code = '''329    # Example Code330    txt = "Hello, welcome to my world."331    x = txt.find("welcome")332    print(x)333    '''334        st.code(example_code, language='python')335        txt = "Hello, welcome to my world."336        x = txt.find("welcome")337        st.write("Example Output:", x)338 339 340import sys341import io342 343st.title("CODE Playground")344 345# Code input area for users to enter their Python code346code = st.text_area("Enter your Python code here:", height=200, value='''# # Hey coder, let's start with some string magic!''')347 348# Button to run the code349if st.button("Run Code"):350    # Redirect stdout to capture print statements351    old_stdout = sys.stdout352    new_stdout = io.StringIO()353    sys.stdout = new_stdout354    355    try:356        # Execute the user's code357        exec(code)358        359        # Get output360        output = new_stdout.getvalue()361        st.text_area("Output:", output, height=150)362    except Exception as e:363        # Handle any errors364        st.error(f"Error: {e}")365    366    # Reset stdout367    sys.stdout = old_stdout368