Split

Thesplit () function splits a string into a list of strings based on occurrences of the regular expression pattern. The string portions that don't match the pattern ate returned as a list of strings. If flags ate require d with the pattern, a regular expression objec t, obtained with compile ( ), must be used in place of the pattern string, or the ( ?iLmsx) sequences (which will be explained later) must be used within the expression. (TPr compiled regular expfeslion's split() method will aclideve the some resuhg )

split() has the following aptuments:

ā€¢ patter nā€” the pattern, as a string, to search for

ā€¢ maxsplit ā€” the maximum number of splits to sake (the default is 0) It returns a list of strings.

from re import *

alist = split(":","OS=Linux:Browser=Netscape:WM=Afterstep")

The output is

OS=Linux

Browser=Netscape WM=Afterstep

Was this article helpful?

0 0

Post a comment