Spaces:
Sleeping
Sleeping
File size: 9,137 Bytes
668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe 668e303 a7d93fe | 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 | 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"
|