import streamlit as st st.title("Python Strings") s = "hello world" st.subheader("Capitalize") st.write("Capitalizes the first character of the string and lowercases all other characters.") st.code(s, language="python") st.code(s.capitalize(), language="python") # Output: "Hello world" s1 = "HELLO World" st.subheader("Casefold") st.write("Converts the string to lowercase, similar to lower(), but more aggressive. It’s designed for case-insensitive comparisons.") st.code(s1, language="python") st.code(s1.casefold(), language="python") # Output: "hello world" s2 = "hello" st.subheader("Center") st.write("Centers the string in a field of a given width, padded with the specified character.") st.code(s2, language="python") st.code(s2.center(10, '*'), language="python") # Output: "**hello***" s3 = "hello world" st.subheader("Count") st.write("Returns the number of non-overlapping occurrences of a substring within the string.") st.code(s3, language="python") st.code(s3.count('o'), language="python") # Output: 2 s4 = "hello" st.subheader("Encode") st.write("Encodes the string using the specified encoding. The default encoding is UTF-8.") st.code(s4, language="python") st.code(s4.encode(), language="python") # Output: b'hello' s5 = "hello world" st.subheader("Endswith") st.write("Returns True if the string ends with the specified suffix; otherwise, returns False.") st.code(s5, language="python") st.code(s5.endswith('world'), language="python") # Output: True s6 = "hello\tworld" st.subheader("Expandtabs") st.write("Replaces tabs in the string with spaces, using the specified tab size.") st.code(s6, language="python") st.code(s6.expandtabs(4), language="python") # Output: "hello world" s7 = "hello world" st.subheader("Find") st.write("Returns the lowest index of the substring if it is found; otherwise, returns -1.") st.code(s7, language="python") st.code(s7.find('world'), language="python") # Output: 6 s8 = "Hello, {}!" st.subheader("Format") st.write("Formats the string using the specified arguments.") st.code(s8, language="python") st.code(s8.format("Alice"), language="python") # Output: "Hello, Alice!" s9 = "hello world" st.subheader("Index") st.write("Returns the lowest index of the substring if it is found; otherwise, raises a ValueError.") st.code(s9, language="python") st.code(s9.index('world'), language="python") # Output: 6 s10 = "hello123" st.subheader("Isalnum") st.write("Returns True if all characters in the string are alphanumeric (letters and numbers) and there is at least one character; otherwise, returns False.") st.code(s10, language="python") st.code(s10.isalnum(), language="python") # Output: True s10 = "hello" st.subheader("Isalpha") st.write("Returns True if all characters in the string are alphabetic and there is at least one character; otherwise, returns False.") st.code(s10, language="python") st.code(s10.isalpha(), language="python") # Output: True s11 = "123" st.subheader("Isdecimal") st.write("Returns True if all characters in the string are decimal characters and there is at least one character; otherwise, returns False.") st.code(s11, language="python") st.code(s11.isdecimal(), language="python") # Output: True s12 = "123" st.subheader("Isdigit") st.write("Returns True if all characters in the string are digits and there is at least one character; otherwise, returns False.") st.code(s12, language="python") st.code(s12.isdigit(), language="python") # Output: True s12 = "hello_world" st.subheader("Isidentifier") st.write("Returns True if the string is a valid identifier (e.g., variable name); otherwise, returns False.") st.code(s12, language="python") st.code(s12.isidentifier(), language="python") # Output: True s13 = "hello" st.subheader("Islower") st.write("Returns True if all characters in the string are lowercase and there is at least one character; otherwise, returns False.") st.code(s13, language="python") st.code(s13.islower(), language="python") # Output: True s14 = "123" st.subheader("Isnumeric") st.write("Returns True if all characters in the string are numeric and there is at least one character; otherwise, returns False.") st.code(s14, language="python") st.code(s14.isnumeric(), language="python") # Output: True s15 = "hello" st.subheader("Isprintable") st.write("Returns True if all characters in the string are printable or the string is empty; otherwise, returns False.") st.code(s15, language="python") st.code(s15.isprintable(), language="python") # Output: True s16 = " " st.subheader("Isspace") st.write("Returns True if all characters in the string are whitespace and there is at least one character; otherwise, returns False.") st.code(s16, language="python") st.code(s16.isspace(), language="python") # Output: True s17 = "Hello World" st.subheader("Istitle") st.write("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.") st.code(s17, language="python") st.code(s17.istitle(), language="python") # Output: True s18 = "HELLO" st.subheader("Isupper") st.write("Returns True if all characters in the string are uppercase and there is at least one character; otherwise, returns False.") st.code(s18, language="python") st.code(s18.isupper(), language="python") # Output: True s19 = "Hello World" st.subheader("Join") st.write("Concatenates the elements of an iterable (like a list) into a single string, with the string as the separator.") st.code(s19, language="python") st.code(' '.join(["Hello", "World"]), language="python") # Output: "Hello World" s20 = "hello" st.subheader("Ljust") st.write("Left-justifies the string in a field of a given width, padded with the specified character.") st.code(s20, language="python") st.code(s20.ljust(10, '*'), language="python") # Output: "hello*****" s20 = "HELLO" st.subheader("Lower") st.write("Converts all characters in the string to lowercase.") st.code(s20, language="python") st.code(s20.lower(), language="python") # Output: "hello" s21 = " hello" st.subheader("Lstrip") st.write("Removes leading characters specified in chars from the string.") st.code(s21, language="python") st.code(s21.lstrip(), language="python") # Output: "hello" s22 = "hello,world" st.subheader("Partition") st.write("Splits the string into a tuple of three elements: the part before the separator, the separator itself, and the part after the separator. If the separator is not found, returns a tuple with the original string and two empty strings.") st.code(s22, language="python") st.code(s22.partition(','), language="python") # Output: ('hello', ',', 'world') s23 = "hello world" st.subheader("Replace") st.write("Replaces occurrences of the substring old with new. If count is specified, it limits the number of replacements.") st.code(s23, language="python") st.code(s23.replace('world', 'there'), language="python") # Output: "hello there" s24 = "hello world" st.subheader("Rfind") st.write("Returns the highest index of the substring if found; otherwise, returns -1.") st.code(s24, language="python") st.code(s24.rfind('o'), language="python") # Output: 7 s25 = "hello world" st.subheader("Rindex") st.write("Returns the highest index of the substring if found; otherwise, raises a ValueError.") st.code(s25, language="python") st.code(s25.rindex('o'), language="python") # Output: 7 s26 = "hello" st.subheader("Rjust") st.write("Right-justifies the string in a field of a given width, padded with the specified character.") st.code(s26, language="python") st.code(s26.rjust(10, '*'), language="python") # Output: "*****hello" s27 = "hello,world,python" st.subheader("Rpartition") st.write("Similar to partition(), but splits from the right side. Returns a tuple of three elements: the part before the last occurrence of the separator, the separator itself, and the part after the separator.") st.code(s27, language="python") st.code(s27.rpartition(','), language="python") # Output: ('hello,world', ',', 'python') s28 = "hello world" st.subheader("Strip") st.write("Removes leading and trailing characters specified in chars from the string.") st.code(s28, language="python") st.code(s28.strip(), language="python") # Output: "hello world" s29 = "hello" st.subheader("Swapcase") st.write("Swaps case, upper case becomes lower case and vice versa.") st.code(s29, language="python") st.code(s29.swapcase(), language="python") # Output: "HELLO" s30 = "hello world" st.subheader("Title") st.write("Converts the first character of each word to uppercase and the remaining characters to lowercase.") st.code(s30, language="python") st.code(s30.title(), language="python") # Output: "Hello World" s31 = "hello world" st.subheader("Upper") st.write("Converts all characters in the string to uppercase.") st.code(s31, language="python") st.code(s31.upper(), language="python") # Output: "HELLO WORLD" s32 = "hello world" st.subheader("Zfill") st.write("Pads the string from the left with zeros to fill a width.") st.code(s32, language="python") st.code(s32.zfill(15), language="python") # Output: "0000000hello world"