O'Reilly logo

Python Essentials by Steven F. Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Splitting, partitioning, and joining strings

In Chapter 2, Simple Data Types, we looked at different processing methods for a string object. We can transform a string into a new string, create strings from non-string data, access a string to determine properties or locations within the string, and parse a string to decompose it.

In many cases, we need to extract elements of a string. The split() method is used to locate repeating list-like structures within a string. The partition() method is used to separate the head and tail of a string.

For example, given a string of the form "numerator=355,denominator=115" we can use these two methods to locate the various names and values. Here's how we can decompose this complex string into pieces:

>>> text="numerator=355,denominator=115" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required