Loading topic
for loop. You do not need to know anything about split or join yet. This chapter 6 min readstr.split(sep) cuts a string at every occurrence of a separator and returns the pieces as a list. It is the first tool most people reach for when they parse text. This chapter covers what it returns for ordinary input and for edge cases, and which mistakes come up in practice.6 min readfor loops. It covers what str.split() does when you give it no separator, and why that differs from split(" ").5 min readstr.split with a separator and with no argument. Here you learn how to stop splitting after a fixed number of cuts, and how to make those cuts from the right-hand end of the string.6 min readstr.split and know that strings are immutable, so every string method returns a new value. By the end you will know what splitlines treats as a line break, how it differs from split("\n"), and5 min readstr.partition and str.rpartition, two methods that cut a string into exactly three parts at one separator. It assumes you can run Python scripts or use the REPL, and that you are comfortable with strings, tuples, tuple unpacking and basic for loops. It also assumes you know5 min readstr.join is the standard way to turn a collection of strings into one string. This chapter covers how it works, what it accepts, and where it surprises people. It assumes you can run Python code, work with lists and tuples, call methods and read their return values, and write a basic for loop or5 min readstr.join from the previous chapter. You should be comfortable with lists, tuples, for loops and generator expressions. Here you will learn why join rejects numbers, how to convert items before joining, and how to handle values such as None, floats, bytes and nes6 min read+= inside a loop is a poor habit and why str.join is the standard replacement. It assumes you know basic loops and generator expressions, that you have used str.join on a list, and that you know strings are immutable, meaning a string can never 6 min readsplit followed by join gives back the original string, and when information is lost along the way. It assumes you know basic Python syntax, how strings, lists and tuples behave, and how to call a method and read its return value. It also assumes you have met str.split,7 min readstr.split handles a fixed separator. Real text often needs a rule instead: "a comma or semicolon, with optional spaces", "any run of digits", "the end of a sentence". re.split from the standard re module splits on a regular expression. This chapter covers how it differs from str.split, how c6 min readstr.split and str.join, including empty fields and maxsplit. It also assumes you can open a file with with open(...) and loop over it. Here you will see where splitting on a separator gives wrong answers, and how the standard csv module fixes that.5 min read