letter ([ww])as you can seepatterns can handle wide variations in datapatt '\ ]*hello\ ]+*)[ww]orldline hello spamworldmobj re match(pattlinemobj group( 'spamnotice that we matched str pattern to str string in the last listing we can also match bytes to bytes in order to handle data such as encoded textbut we cannot mix the two string types ( constraint which is true in python in general--python wouldn' have the encoding information needed to know how to convert between the raw bytes and the unicode text)patt '\ ]*hello\ ]+*)[ww]orldline bhello spamworldre match(pattlinegroup( 'spamboth as bytes works too and returns bytes groups but cannot mix str/bytes re match(patthello spamworld'typeerrorcan' use bytes pattern on string-like object re match('\ ]*hello\ ]+*)[ww]orld'linetypeerrorcan' use string pattern on bytes-like object in addition to the tools these examples demonstratethere are methods for scanning ahead to find match (search)scanning to find all matches (findall)splitting and replacing on patternsand so on all have analogous module and precompiled call forms the next section turns to few examples to demonstrate more of the basics string operations versus patterns notice how the preceding example skips optional whitespace and allows for uppercase or lowercase letters this underscores why you may want to use patterns in the first place--they support more general kinds of text than string object methods can here' another case in pointwe've seen that string methods can split on and replace substringbut they don' suffice if the delimiter might be more than one alternative'aaa--bbb--cccsplit('--'['aaa''bbb''ccc''aaa--bbb--cccreplace('--'''aaa bbb cccstring methods use fixed strings 'aaa--bbb==cccsplit(['--''==']typeerrorcan' convert 'listobject to str implicitly 'aaa--bbb==cccreplace(['--''==']'typeerrorcan' convert 'listobject to str implicitly patterns can do similar workbut also can handle alternatives directlyby virtue of their pattern matching syntax in the followingthe syntax --|=matches either string -or string ==the syntax [-=matches either the character or ( character set)and the text and language