CoolFace
Apppublic

Naveenvodnala/String_methods

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py838 linesDownload Raw Back to root
1import streamlit as st 2 3st.image('https://i.ytimg.com/vi/crw3rVFNwIM/maxresdefault.jpg')4 5st.title('String Methods')6 7selectbox=st.selectbox(label='choose a string method',8            options=['capitalize',9 'casefold',10 'count','center',11 'endswith',12 'expandtabs',13 'find',14 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip','partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',  'startswith', 'strip', 'swapcase', 'title', 'upper','encode', 'zfill'])15 16if st.button(f'click here to learn about {selectbox} '):17 18    if selectbox == 'casefold':19        st.markdown('''20          ### casefold() 21          22            **Syntax:** ```str.casefold()```23            24            **Description:** Converts the string to lowercase, more aggressive than ```lower()```.25            26            **Example:**27            28            ```python29                text = "HELLO"30                print(text.casefold())31            ```32            33            **Output:**34            35            `hello`36        ''')37    elif selectbox == 'capitalize':38        st.markdown('''39            ### capitalize()40            41            **Syntax:** `str.capitalize()`42            43            **Description:** Converts the first character to `uppercase`.44            45            **Example:**46            47            ```python48                text = "hello world"49                print(text.capitalize())50            ```51            52            **Output:**53            54            ```Hello world```55        ''')56    elif selectbox == 'center':57        st.markdown('''58            ### center()59            60            **Syntax:** `str.center(width[, fillchar])`61            62            **Description:** Centers the string, padding with spaces or a specified character.63            64            **Example:**65            66            ```python67                text = "hello"68                print(text.center(10, '-'))69            ```70            71            **Output:**72            73            `--hello---`74        ''')    75 76    elif selectbox == 'encode':77        st.markdown('''78            ### encode()79            80            **Syntax:** `str.encode(encoding='utf-8', errors='strict')`81            82            **Description:** Encodes the string using a specified encoding.83            84            **Example:**85            86            ```python87                text = "hello"88                print(text.encode())89            ```90            91            **Output:**92            93            ```b'hello'```94        ''')95    elif selectbox == 'endswith':96        st.markdown('''97            ### endswith()98            99            **Syntax:** `string.endswith(value, start, end)`100            101            **Description:** The endswith() method returns `True` if the string ends with the specified value.102            103            **Example:**104            105            ```python106                txt = "Hello, welcome to my world."107                x = txt.endswith("my world.")108                print(x)109            ```110            111            **Output:**112            113            ```True```114        ''')115    elif selectbox == 'expandtabs':116        st.markdown('''117            ### expandtabs()118            119            **Syntax:** `str.expandtabs(tabsize=8)`120            121            **Description:** Replaces tabs with spaces, using a tab size of `tabsize`.122            123            **Example:**124            125            ```python126                text = "hello\tworld"127                result = text.expandtabs(4)128                print(result)129            ```130            131            **Output:**132            133            ```hello   world```134        ''')135    elif selectbox == 'find':136        st.markdown('''137            ### find()138            139            **Syntax:** `str.find(substring, start, end)`140            141            **Description:** Searches the string for a specified value and returns the position of where it was found.142            143            **Example:**144            145            ```python146                text = "hello world"147                result = text.find("world")148                print(result)149            ```150            151            **Output:**152            153            ```6```154        ''')155        156    elif selectbox == 'index':157        st.markdown('''158            ### index()159            160            **Syntax:** `str.expandtabs(tabsize=8)`161            162            **Description:** 	Searches the string for a specified value and returns the position of where it was found.163            164            **Example:**165            166            ```python167                txt = "Hello, welcome to my world."168 169                x = txt.index("welcome")170                171                print(x)172            ```173            174            **Output:**175            176            ```7```177        ''')178    elif selectbox == 'isalnum':179        st.markdown('''180            ### isalnum()181            182            **Syntax:** `str.isalnum()`183            184            **Description:** Returns `True` if all characters are alphanumeric (letters or digits).185            186            **Example:**187            188            ```python189                text = "hello123"190                result = text.isalnum()191                print(result)192            ```193            194            **Output:**195            196            ```True```197        ''')198    elif selectbox == 'isalpha':199        st.markdown('''200            ### isalpha()201            202            **Syntax:** `str.isalpha()`203            204            **Description:** Returns `True` if all characters are alphabetic.205            206            **Example:**207            208            ```python209                text = "hello"210                result = text.isalpha()211                print(result)212            ```213            214            **Output:**215            216            ```True```217        ''')218    elif selectbox == 'isascii':219        st.markdown('''220            ### isascii()221            222            **Syntax:** `str.isascii()`223            224            **Description:** Returns `True` if all characters are ASCII.225            226            **Example:**227            228            ```python229                text = "hello"230                result = text.isascii()231                print(result)232            ```233            234            **Output:**235            236            ```True```237        ''')238    elif selectbox == 'isdigit':239        st.markdown('''240            ### isdigit()241            242            **Syntax:** `str.isdigit()`243            244            **Description:** Returns `True` if all characters are digits.245            246            **Example:**247            248            ```python249                text = "12345"250                result = text.isdigit()251                print(result)252            ```253            254            **Output:**255            256            ```True```257        ''')258    elif selectbox == 'isdecimal':259        st.markdown('''260            ### isdecimal()261            262            **Syntax:** `str.isdecimal()`263            264            **Description:** Returns `True` if all characters are decimals.265            266            **Example:**267            268            ```python269                text = "12345"270                result = text.isdecimal()271                print(result)272            ```273            274            **Output:**275            276            ```True```277        ''')278    elif selectbox == 'isidentifier':279        st.markdown('''280            ### isidentifier()281            282            **Syntax:** `str.isidentifier()`283            284            **Description:** Returns `True` if the string is a valid Python identifier.285            286            **Example:**287            288            ```python289                text = "variable_name"290                result = text.isidentifier()291                print(result)292            ```293            294            **Output:**295            296            ```True```297        ''')298    elif selectbox == 'islower':299        st.markdown('''300            ### islower()301            302            **Syntax:** `str.islower()`303            304            **Description:** Returns `True` if all alphabetic characters are lowercase.305            306            **Example:**307            308            ```python309                text = "hello"310                result = text.islower()311                print(result)312            ```313            314            **Output:**315            316            ```True```317        ''')318    elif selectbox == 'isnumeric':319        st.markdown('''320            ### isnumeric()321            322            **Syntax:** `str.isnumeric()`323            324            **Description:** Returns `True` if all characters are numeric.325            326            **Example:**327            328            ```python329               text = "12345"330                result = text.isnumeric()331                print(result)332            ```333            334            **Output:**335            336            ```True```337        ''')338    elif selectbox == 'isprintable':339        st.markdown('''340            ### isprintable()341            342            **Syntax:** `str.isprintable()`343            344            **Description:** Returns `True` if all characters are printable.345            346            **Example:**347            348            ```python349                text = "hello\nworld"350                result = text.isprintable()351                print(result)352            ```353            354            **Output:**355            356            ```False```357        ''')358    elif selectbox == 'isspace':359        st.markdown('''360            ### isspace()361            362            **Syntax:** `str.isspace()`363            364            **Description:** Returns `True` if the string contains only whitespace characters.365            366            **Example:**367            368            ```python369               text = "   "370                result = text.isspace()371                print(result)372            ```373            374            **Output:**375            376            ```True```377        ''')378    elif selectbox == 'istitle':379        st.markdown('''380            ### istitle()381            382            **Syntax:** `str.istitle()`383            384            **Description:** Returns `True` if the string is titlecased (each word starts with an uppercase letter).385            386            **Example:**387            388            ```python389                text = "Hello World"390                result = text.istitle()391                print(result)392            ```393            394            **Output:**395            396            ```True```397        ''')398    elif selectbox == 'isupper':399        st.markdown('''400            ### isupper()401            402            **Syntax:** `str.isupper()`403            404            **Description:** Returns `True` if all alphabetic characters are uppercase.405            406            **Example:**407            408            ```python409                text = "HELLO"410                result = text.isupper()411                print(result)412            ```413            414            **Output:**415            416            ```True```417        ''')418    elif selectbox == 'join':419        st.markdown('''420            ### join()421            422            **Syntax:** `str.join(iterable)`423            424            **Description:** Concatenates the elements of an iterable (such as a list) into a string, with the string acting as a separator.425            426            **Example:**427            428            ```python429                separator = ", "430                fruits = ["apple", "banana", "cherry"]431                result = separator.join(fruits)432                print(result)433            ```434            435            **Output:**436            437            ```apple, banana, cherry```438        ''')439    elif selectbox == 'ljust':440        st.markdown('''441            ### expandtabs()442            443            **Syntax:** `str.ljust(width[, fillchar])`444            445            **Description:** Returns the string left-justified in a string of the specified width. Optional fill characters can be added.446            447            **Example:**448            449            ```python450                text = "Hello"451                result = text.ljust(10, '-')452                print(result)453            ```454            455            **Output:**456            457            ```Hello-----```458        ''')459    elif selectbox == 'lower':460        st.markdown('''461            ### lower()462            463            **Syntax:** `str.lower()`464            465            **Description:** Converts all characters in the string to lowercase.466            467            **Example:**468            469            ```python470                text = "HELLO WORLD"471                result = text.lower()472                print(result)473            ```474            475            **Output:**476            477            ```hello world```478        ''')479    elif selectbox == 'lstrip':480        st.markdown('''481            ### lstrip()482            483            **Syntax:** `str.lstrip([chars])`484            485            **Description:** Removes leading characters (default is whitespace) from the left side of the string.486            487            **Example:**488            489            ```python490                text = "    Hello"491                result = text.lstrip()492                print(f"'{result}'")493            ```494            495            **Output:**496            497            ```'Hello'```498        ''')499    elif selectbox == 'partition':500        st.markdown('''501            ### partition()502            503            **Syntax:** `str.partition(sep)`504            505            **Description:** Splits the string into three parts: the part before the separator, the separator itself, and the part after the separator. If the separator is not found, returns the string itself, followed by two empty strings.506            507            **Example:**508            509            ```python510                text = "I love Python programming"511                result = text.partition("Python")512                print(result)513            ```514            515            **Output:**516            517            ```('I love ', 'Python', ' programming')```518        ''')519    elif selectbox == 'removeprefix':520        st.markdown('''521            ### removeprefix()522            523            **Syntax:** `str.removeprefix(prefix)`524            525            **Description:** Removes the specified prefix from the string, if present.526            527            **Example:**528            529            ```python530               text = "HelloWorld"531                result = text.removeprefix("Hello")532                print(result)533            ```534            535            **Output:**536            537            ```World```538        ''')539    elif selectbox == 'removesuffix':540        st.markdown('''541            ### removesuffix()542            543            **Syntax:** `str.removesuffix(suffix)`544            545            **Description:** Removes the specified suffix from the string, if present546            547            **Example:**548            549            ```python550                text = "WorldHello"551                result = text.removesuffix("Hello")552                print(result)553            ```554            555            **Output:**556            557            ```World```558        ''')559    elif selectbox == 'replace':560        st.markdown('''561            ### replace()562            563            **Syntax:** `str.replace(old, new[, count])`564            565            **Description:** Replaces all occurrences of a specified substring with another substring. You can also limit the number of replacements.566            567            **Example:**568            569            ```python570                text = "I love Python programming"571                result = text.replace("Python", "Java")572                print(result)573            ```574            575            **Output:**576            577            ```I love Java programming```578        ''')579    elif selectbox == 'rfind':580        st.markdown('''581            ### rfind()582            583            **Syntax:** `str.rfind(sub[, start[, end]])`584            585            **Description:**  Returns the highest index of the specified substring. Returns -1 if the substring is not found.586            587            **Example:**588            589            ```python590                text = "Welcome to Python programming"591                result = text.rfind("Python")592                print(result)593            ```594            595            **Output:**596            597            ```11```598        ''')599    elif selectbox == 'rindex':600        st.markdown('''601            ### rindex()602            603            **Syntax:** `str.rindex(sub[, start[, end]])`604            605            **Description:** Returns the highest index of the specified substring. Raises a ValueError if the substring is not found.606            607            **Example:**608            609            ```python610                text = "Welcome to Python programming"611                result = text.rindex("Python")612                print(result)613            ```614            615            **Output:**616            617            ```11```618        ''')619    elif selectbox == 'rjust':620        st.markdown('''621            ### rjust()622            623            **Syntax:** `str.rjust(width[, fillchar])`624            625            **Description:** Returns the string right-justified in a string of specified width. Optional fill characters can be added.626            627            **Example:**628            629            ```python630                text = "Hello"631                result = text.rjust(10, '-')632                print(result)633            ```634            635            **Output:**636            637            ```-----Hello```638        ''')639    elif selectbox == 'rpartition':640        st.markdown('''641            ### rpartition()642            643            **Syntax:** `str.rpartition(sep)`644            645            **Description:** Splits the string into a tuple containing three parts: the part before the separator, the separator itself, and the part after.646            647            **Example:**648            649            ```python650                text = "I love Python programming"651                result = text.rpartition("Python")652                print(result)653            ```654            655            **Output:**656            657            ```('I love ', 'Python', ' programming')```658        ''')659    elif selectbox == 'rsplit':660        st.markdown('''661            ### rsplit()662            663            **Syntax:** `str.rsplit(sep=None, maxsplit=-1)`664            665            **Description:** Splits the string at the specified separator and returns a list. Optionally, you can limit the number of splits.666            667            **Example:**668            669            ```python670                text = "apple, banana, cherry"671                result = text.rsplit(", ", 1)672                print(result)673            ```674            675            **Output:**676            677            ```['apple, banana', 'cherry']```678        ''')679    elif selectbox == 'rstrip':680        st.markdown('''681            ### rstrip()682            683            **Syntax:** `str.rstrip([chars])`684            685            **Description:** Removes trailing characters (default is whitespace) from the right side of the string.686            687            **Example:**688            689            ```python690                text = "Hello    "691                result = text.rstrip()692                print(f"'{result}'")693            ```694            695            **Output:**696            697            ```'Hello'```698        ''')699    elif selectbox == 'split':700        st.markdown('''701            ### split()702            703            **Syntax:** `str.split(sep=None, maxsplit=-1)`704            705            **Description:** Splits the string at the specified separator and returns a list. Optionally, you can limit the number of splits.706            707            **Example:**708            709            ```python710                text = "apple, banana, cherry"711                result = text.split(", ")712                print(result)713            ```714            715            **Output:**716            717            ```['apple', 'banana', 'cherry']```718        ''')719    elif selectbox == 'startswith':720        st.markdown('''721            ### startswith()722            723            **Syntax:** `str.startswith(prefix[, start[, end]])`724            725            **Description:** Returns `True` if the string starts with the specified prefix; otherwise, returns `False`.726            727            **Example:**728            729            ```python730                text = "Hello, world!"731                result = text.startswith("Hello")732                print(result)733            ```734            735            **Output:**736            737            ```True```738        ''')739    elif selectbox == 'strip':740        st.markdown('''741            ### strip()742            743            **Syntax:** `str.strip([chars])`744            745            **Description:** Removes leading and trailing characters (default is whitespace) from both sides of the string.746            747            **Example:**748            749            ```python750                text = "   Hello   "751                result = text.strip()752                print(f"'{result}'")753            ```754            755            **Output:**756            757            ```'Hello'```758        ''')759    elif selectbox == 'swapcase':760        st.markdown('''761            ### swapcase()762            763            **Syntax:** `str.swapcase()`764            765            **Description:** Swaps the case of each character in the string. Lowercase becomes uppercase, and vice versa.766            767            **Example:**768            769            ```python770                text = "Hello World"771                result = text.swapcase()772                print(result)773            ```774            775            **Output:**776            777            ```hELLO wORLD```778        ''')779    elif selectbox == 'title':780        st.markdown('''781            ### title()782            783            **Syntax:** `str.title()`784            785            **Description:**  Converts the first character of each word to uppercase and the rest to lowercase.786            787            **Example:**788            789            ```python790                text = "hello world"791                result = text.title()792                print(result)793            ```794            795            **Output:**796            797            ```Hello World```798        ''')799    elif selectbox == 'upper':800        st.markdown('''801            ### upper()802            803            **Syntax:** `str.upper()`804            805            **Description:** Converts all characters to uppercase.806            807            **Example:**808            809            ```python810                text = "hello"811                result = text.upper()812                print(result)813            ```814            815            **Output:**816            817            ```HELLO```818        ''')819    elif selectbox == 'zfill':820        st.markdown('''821            ### zfill()822            823            **Syntax:** `str.zfill(width)`824            825            **Description:** Pads the string on the left with zeros to fill a specified width.826            827            **Example:**828            829            ```python830                text = "42"831                result = text.zfill(5)832                print(result)833            ```834            835            **Output:**836            837            ```00042```838        ''')