Learn How to Code with Python: Beginner to Advanced Guide
In Python, strings are sequences of characters. Because they are sequences, they support indexing and slicing. Slicing allows you to extract a portion of a string by specifying a start index and an end index. The syntax follows the pattern `string[start:end]`, where the start index is inclusive and the end index is exclusive. For example, if you have the string `"Programming"`, using `"Programming"[0:6]` returns `"Progra"`. The character at index 6 is not included. If you omit the start index, Python assumes 0. If you omit the end index, Python slices until the end of the string. Slicing also supports a third parameter called step: `string[start:end:step]`. This allows you to skip characters. For example, `"Programming"[::2]` returns every second character from the entire string. 1 Pyt yth ytho What does `"Python"[1:4]` return? Negative indexing allows you to count characters from the end of the string. In Python, `-1` represents the last character, `-2` the second-to-last, and so on. This is extremely useful when you don’t know the exact length of a string but want to access the end. For example, `"Developer"[-1]` returns `"r"`. If you slice using negative indices, such as `"Developer"[-4:-1]`, you get a substring starting four characters from the end and stopping before the last character. Negative slicing follows the same rules: start is inclusive, end is exclusive. Understanding this eliminates many off-by-one errors. 1 Cod ing din What does `"Coding"[-3:]` return? The `split()` method divides a string into a list of substrings based on a separator. By default, `split()` separates using whitespace. This means spaces, tabs, and newline characters are considered boundaries. For example, `"Learn Python Fast".split()` returns `["Learn", "Python", "Fast"]`. Each word becomes a separate element in a list. You can also provide a custom separator. For instance, `"apple,banana,orange".split(",")` returns `["apple", "banana", "orange"]`. 1 ["one-two-three"] ["one", "two", "three"] ["one", "two-three"] What does `"one-two-three".split("-")` return? The `split()` method can also take a second argument: `maxsplit`. This limits the number of splits performed. The syntax is `split(separator, maxsplit)`. For example, `"a,b,c,d".split(",", 1)` returns `["a", "b,c,d"]`. Python performs only one split and stops. This is useful when parsing structured text where only the first few separators matter. 0 ["x", "y", "z"] ["x", "y:z"] ["x:y", "z"] What does `"x:y:z".split(":", 2)` return? The `replace()` method creates a new string where occurrences of a substring are replaced with another substring. Strings are immutable in Python, so `replace()` does not modify the original string; it returns a new one. For example, `"I like cats".replace("cats", "dogs")` returns `"I like dogs"`. The original string remains unchanged. You can also replace multiple occurrences. `"aaa".replace("a", "b")` returns `"bbb"`. 0 bonono bonana banana What does `"banana".replace("a", "o")` return? The `replace()` method also accepts a third parameter: the maximum number of replacements. The syntax is `replace(old, new, count)`. For example, `"apple apple apple".replace("apple", "orange", 2)` returns `"orange orange apple"`. Only the first two occurrences are replaced. This gives you precise control when working with repetitive text patterns. 1 done done done done test test test done test What does `"test test test".replace("test", "done", 1)` return? Slicing can reverse a string by using a negative step. The syntax `string[::-1]` starts from the end and moves backward through the entire string. For example, `"Python"[::-1]` returns `"nohtyP"`. This works because the step value of `-1` instructs Python to move one character backward at each step. Reversing strings is useful in palindrome checks and text manipulation tasks. 1 abcde edcba bcdea What does `"abcde"[::-1]` return? Combining `split()` and slicing allows powerful text processing. For example, `"2026-02-25".split("-")[0:2]` first splits the string into `["2026", "02", "25"]` and then slices the list to keep only the first two elements. Remember: `split()` returns a list, not a string. After splitting, you work with list indexing and slicing rules. This combination is common when processing dates, CSV data, and log files. 1 red blue green What is the result of `"red,blue,green".split(",")[1]`? Slicing does not cause errors if the end index exceeds the string length. Python simply stops at the end. For example, `"code"[0:10]` returns `"code"` without raising an exception. This makes slicing safer compared to direct indexing, which would cause an IndexError if you access a position that does not exist. Understanding this behavior helps you write more robust programs. 0 ata data dat What does `"data"[1:10]` return? String methods can be chained together. For example, `" hello world ".strip().replace("world", "Python").split()` first removes surrounding spaces, then replaces a word, and finally splits the string into a list. The result of this chain is `["hello", "Python"]`. Each method returns a new value, which the next method uses. Method chaining is powerful because it allows complex transformations in a single line. 1 ["a", "b", "c"] ["abc"] ["a b c"] What does `"a b c".replace(" ", "").split()` return?