8/28/26About 1 minAbout 211 words
Concept: String operators & functions
Strings support the following operators and functions:
| Operator / Function | Description | Example | Result |
|---|---|---|---|
+ | Concatenates two strings. | "Hello" + " World" | "Hello World" |
* | Repeats a string a specified number of times. | "Hello" * 3 | "HelloHelloHello" |
in | Checks if a substring exists within a string (case sensitve) | "Hello" in "Hello World" "hello" in "Hello World" | True False |
len() | Returns the length of a string (the number of characters) | len("Hello") | 5 |
[] | Accesses a character at a specific index (0-based) | "Hello"[1] | "e" |
f | Formats a string using f-strings | name = "Alice" f"Hello, {name}!" | "Hello, Alice!" |
upper() | Converts all characters in a string to uppercase | "Hello".upper() | "HELLO" |
lower() | Converts all characters in a string to lowercase | "Hello".lower() | "hello" |
strip() | Removes leading and trailing whitespace from a string | " Hello ".strip() | "Hello" |
replace() | Replaces occurrences of a substring with another substring | "Hello World".replace("World", "Universe") | "Hello Universe" |
split() | Splits a string into a list of substrings based on a delimiter | "Hello World".split(" ")[1] | ["Hello", "World"] |
join() | Joins a list of strings into a single string using a delimiter | " ".join(["Hello", "World"]) | "Hello World" |