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
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import streamlit as st
import io
import sys

# Function to display the example and explanation of a selected string method
def show_string_method_example(method_name):
    if method_name == "Capitalize":
        st.subheader("Capitalize")
        st.markdown("""
        **Description:**  
        Capitalizes the first character of the string and lowercases all other characters.
        
        **Example:**  
        ```
        s = "hello world"
        s.capitalize()  # "Hello world"
        ```
        """)
    elif method_name == "Casefold":
        st.subheader("Casefold")
        st.markdown("""
        **Description:**  
        Converts the string to lowercase, similar to `lower()`, but more aggressive. It’s designed for case-insensitive comparisons.
        
        **Example:**  
        ```
        s = "HELLO World"
        s.casefold()  # "hello world"
        ```
        """)
    elif method_name == "Center":
        st.subheader("Center")
        st.markdown("""
        **Description:**  
        Centers the string in a field of a given width, padded with the specified character.
        
        **Example:**  
        ```
        s = "hello"
        s.center(10, '*')  # "**hello***"
        ```
        """)
    elif method_name == "Count":
        st.subheader("Count")
        st.markdown("""
        **Description:**  
        Returns the number of non-overlapping occurrences of a substring within the string.
        
        **Example:**  
        ```
        s = "hello world"
        s.count('o')  # 2
        ```
        """)
    elif method_name == "Encode":
        st.subheader("Encode")
        st.markdown("""
        **Description:**  
        Encodes the string using the specified encoding. The default encoding is UTF-8.
        
        **Example:**  
        ```
        s = "hello"
        s.encode()  # b'hello'
        ```
        """)
    elif method_name == "Endswith":
        st.subheader("Endswith")
        st.markdown("""
        **Description:**  
        Returns True if the string ends with the specified suffix; otherwise, returns False.
        
        **Example:**  
        ```
        s = "hello world"
        s.endswith('world')  # True
        ```
        """)
    elif method_name == "Expandtabs":
        st.subheader("Expandtabs")
        st.markdown("""
        **Description:**  
        Replaces tabs in the string with spaces, using the specified tab size.
        
        **Example:**  
        ```
        s = "hello\tworld"
        s.expandtabs(4)  # "hello   world"
        ```
        """)
    elif method_name == "Find":
        st.subheader("Find")
        st.markdown("""
        **Description:**  
        Returns the lowest index of the substring if it is found; otherwise, returns -1.
        
        **Example:**  
        ```
        s = "hello world"
        s.find('world')  # 6
        ```
        """)
    elif method_name == "Format":
        st.subheader("Format")
        st.markdown("""
        **Description:**  
        Formats the string using the specified arguments.
        
        **Example:**  
        ```
        s = "Hello, {}!"
        s.format("Alice")  # "Hello, Alice!"
        ```
        """)
    elif method_name == "Index":
        st.subheader("Index")
        st.markdown("""
        **Description:**  
        Returns the lowest index of the substring if it is found; otherwise, raises a ValueError.
        
        **Example:**  
        ```
        s = "hello world"
        s.index('world')  # 6
        ```
        """)
    elif method_name == "Isalnum":
        st.subheader("Isalnum")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are alphanumeric (letters and numbers) and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "hello123"
        s.isalnum()  # True
        ```
        """)
    elif method_name == "Isalpha":
        st.subheader("Isalpha")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are alphabetic and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "hello"
        s.isalpha()  # True
        ```
        """)
    elif method_name == "Isdecimal":
        st.subheader("Isdecimal")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are decimal characters and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "123"
        s.isdecimal()  # True
        ```
        """)
    elif method_name == "Isdigit":
        st.subheader("Isdigit")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are digits and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "123"
        s.isdigit()  # True
        ```
        """)
    elif method_name == "Isidentifier":
        st.subheader("Isidentifier")
        st.markdown("""
        **Description:**  
        Returns True if the string is a valid identifier (e.g., variable name); otherwise, returns False.
        
        **Example:**  
        ```
        s = "hello_world"
        s.isidentifier()  # True
        ```
        """)
    elif method_name == "Islower":
        st.subheader("Islower")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are lowercase and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "hello"
        s.islower()  # True
        ```
        """)
    elif method_name == "Isnumeric":
        st.subheader("Isnumeric")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are numeric and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "123"
        s.isnumeric()  # True
        ```
        """)
    elif method_name == "Isprintable":
        st.subheader("Isprintable")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are printable or the string is empty; otherwise, returns False.
        
        **Example:**  
        ```
        s = "hello"
        s.isprintable()  # True
        ```
        """)
    elif method_name == "Isspace":
        st.subheader("Isspace")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are whitespace and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "   "
        s.isspace()  # True
        ```
        """)
    elif method_name == "Istitle":
        st.subheader("Istitle")
        st.markdown("""
        **Description:**  
        Returns True if the string is in title case (i.e., each word starts with an uppercase letter and the rest are lowercase); otherwise, returns False.
        
        **Example:**  
        ```
        s = "Hello World"
        s.istitle()  # True
        ```
        """)
    elif method_name == "Isupper":
        st.subheader("Isupper")
        st.markdown("""
        **Description:**  
        Returns True if all characters in the string are uppercase and there is at least one character; otherwise, returns False.
        
        **Example:**  
        ```
        s = "HELLO"
        s.isupper()  # True
        ```
        """)
    elif method_name == "Join":
        st.subheader("Join")
        st.markdown("""
        **Description:**  
        Concatenates the elements of an iterable (like a list) into a single string, with the string as the separator.
        
        **Example:**  
        ```
        s = " "
        s.join(["Hello", "World"])  # "Hello World"
        ```
        """)
    elif method_name == "Ljust":
        st.subheader("Ljust")
        st.markdown("""
        **Description:**  
        Left-justifies the string in a field of a given width, padded with the specified character.
        
        **Example:**  
        ```
        s = "hello"
        s.ljust(10, '*')  # "hello*****"
        ```
        """)
    elif method_name == "Lower":
        st.subheader("Lower")
        st.markdown("""
        **Description:**  
        Converts all characters in the string to lowercase.
        
        **Example:**  
        ```
        s = "HELLO"
        s.lower()  # "hello"
        ```
        """)
    elif method_name == "Lstrip":
        st.subheader("Lstrip")
        st.markdown("""
        **Description:**  
        Removes leading characters specified in chars from the string.
        
        **Example:**  
        ```
        s = "   hello"
        s.lstrip()  # "hello"
        ```
        """)
    elif method_name == "Rfind":
        st.subheader("Rfind")
        st.markdown("""
        **Description:**  
        Returns the highest index of the substring if it is found; otherwise, returns -1.
        
        **Example:**  
        ```
        s = "hello world"
        s.rfind('o')  # 7
        ```
        """)
    elif method_name == "Rindex":
        st.subheader("Rindex")
        st.markdown("""
        **Description:**  
        Returns the highest index of the substring if it is found; otherwise, raises a ValueError.
        
        **Example:**  
        ```
        s = "hello world"
        s.rindex('o')  # 7
        ```
        """)
    elif method_name == "Rjust":
        st.subheader("Rjust")
        st.markdown("""
        **Description:**  
        Right-justifies the string in a field of a given width, padded with the specified character.
        
        **Example:**  
        ```
        s = "hello"
        s.rjust(10, '*')  # "*****hello"
        ```
        """)
    elif method_name == "Rpartition":
        st.subheader("Rpartition")
        st.markdown("""
        **Description:**  
        Splits the string into a tuple of three elements: the part before the separator, the separator itself, and the part after the separator. Searches for the separator from the end of the string.
        
        **Example:**  
        ```
        s = "hello,world,again"
        s.rpartition(',')  # ('hello,world', ',', 'again')
        ```
        """)
    elif method_name == "Rsplit":
        st.subheader("Rsplit")
        st.markdown("""
        **Description:**  
        Splits the string from the end, using the specified separator and returning a list of the words. If maxsplit is specified, it limits the number of splits.
        
        **Example:**  
        ```
        s = "a,b,c,d"
        s.rsplit(',', 2)  # ['a,b', 'c', 'd']
        ```
        """)
    elif method_name == "Rstrip":
        st.subheader("Rstrip")
        st.markdown("""
        **Description:**  
        Removes trailing characters specified in chars from the string.
        
        **Example:**  
        ```
        s = "hello   "
        s.rstrip()  # "hello"
        ```
        """)
    elif method_name == "Split":
        st.subheader("Split")
        st.markdown("""
        **Description:**  
        Splits the string using the specified separator and returns a list of the words. If maxsplit is specified, it limits the number of splits.
        
        **Example:**  
        ```
        s = "a,b,c,d"
        s.split(',')  # ['a', 'b', 'c', 'd']
        ```
        """)
    elif method_name == "Splitlines":
        st.subheader("Splitlines")
        st.markdown("""
        **Description:**  
        Splits the string at line breaks and returns a list of the lines.
        
        **Example:**  
        ```
        s = "hello\nworld"
        s.splitlines()  # ['hello', 'world']
        ```
        """)
    elif method_name == "Startswith":
        st.subheader("Startswith")
        st.markdown("""
        **Description:**  
        Returns True if the string starts with the specified prefix; otherwise, returns False.
        
        **Example:**  
        ```
        s = "hello world"
        s.startswith('hello')  # True
        ```
        """)
    elif method_name == "Strip":
        st.subheader("Strip")
        st.markdown("""
        **Description:**  
        Removes leading and trailing characters specified in chars from the string.
        
        **Example:**  
        ```
        s = "   hello   "
        s.strip()  # "hello"
        ```
        """)
    elif method_name == "Swapcase":
        st.subheader("Swapcase")
        st.markdown("""
        **Description:**  
        Converts uppercase characters to lowercase and lowercase characters to uppercase.
        
        **Example:**  
        ```
        s = "Hello World"
        s.swapcase()  # "hELLO wORLD"
        ```
        """)
    elif method_name == "Title":
        st.subheader("Title")
        st.markdown("""
        **Description:**  
        Converts the first character of each word to uppercase and the rest to lowercase.
        
        **Example:**  
        ```
        s = "hello world"
        s.title()  # "Hello World"
        ```
        """)
    elif method_name == "Upper":
        st.subheader("Upper")
        st.markdown("""
        **Description:**  
        Converts all characters in the string to uppercase.
        
        **Example:**  
        ```
        s = "hello"
        s.upper()  # "HELLO"
        ```
        """)
    elif method_name == "Zfill":
        st.subheader("Zfill")
        st.markdown("""
        **Description:**  
        Pads the string with zeros on the left, to fill the specified width.
        
        **Example:**  
        ```
        s = "42"
        s.zfill(5)  # "00042"
        ```
        """)
    else:
        st.write("Please select a valid string method.")


# List of string methods
methods = [
    "Capitalize", "Casefold", "Center", "Count", "Encode", "Endswith", 
    "Expandtabs", "Find", "Format", "Index", "Isalnum", "Isalpha", 
    "Isdecimal", "Isdigit", "Isidentifier", "Islower", "Isnumeric", 
    "Isprintable", "Isspace", "Istitle", "Isupper", "Join", "Ljust", 
    "Lower", "Lstrip", "Partition", "Replace", "Rfind", "Rindex", 
    "Rjust", "Rpartition", "Rsplit", "Rstrip", "Split", "Splitlines", 
    "Startswith", "Strip", "Swapcase", "Title", "Upper", "Zfill"
]

def execute_user_code():
    st.subheader("Try it Yourself")
    code_input = st.text_area("Write your Python code here:", height=200)
    if st.button("Run Code"):
        try:
            # Capture the output
            original_stdout = sys.stdout  # Save a reference to the original standard output
            result = io.StringIO()        # Create a string buffer to capture the output
            sys.stdout = result           # Redirect standard output to the string buffer
            exec(code_input)              # Execute the user-provided code
            sys.stdout = original_stdout  # Reset the standard output to its original value
            st.text("Output:")
            st.code(result.getvalue())    # Display the captured output
        except Exception as e:
            st.error(f"An error occurred: {e}")



# Streamlit app layout
st.title("Python String Methods Explorer")
selected_method = st.selectbox("Select a String Method", methods)
show_string_method_example(selected_method)
# In your main Streamlit app code
execute_user_code()