File size: 7,569 Bytes
4d1d479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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}`")