# String processing in Python

**URL:** <https://boards.straightdope.com/t/string-processing-in-python/437655>\
**Category:** Factual Questions\
**Created:** [February 14, 2008, 9:11pm UTC](https://boards.straightdope.com/t/string-processing-in-python/437655 "2008-02-14T21:11:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 14, 2008, 9:11pm UTC](https://boards.straightdope.com/t/string-processing-in-python/437655/1 "2008-02-14T21:11:41Z")

</div>

I’m trying to teach myself Python, and I’ve got a reasonably simple task set out for myself. Basically, I want to read in a comma-delimited file, get the individual fields, and do some stuff with them. In Perl, what I’m trying to do is fairly simple:

```auto

if ($s =~ m/(\S+)\,(\S+)\,(\S+)\,(\S+)/)
{
    $a = $2;
    $b = $3;
    $c = $4;
}

```

What’s the equivalent Python code to do this?

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [February 14, 2008, 9:17pm UTC](https://boards.straightdope.com/t/string-processing-in-python/437655/2 "2008-02-14T21:17:00Z")

</div>

Oy vey. First of all, you’d do much better using [split](http://perldoc.perl.org/functions/split.html) in Perl for that task. Python also has a split method which you can call on any string object. To split a string foo on commas:

foo.split(‘,’)

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 14, 2008, 11:36pm UTC](https://boards.straightdope.com/t/string-processing-in-python/437655/3 "2008-02-14T23:36:56Z")

</div>

split() works here, but suppose that I was trying to do something a bit more complicated:

```auto

if ($s =~ m/\((\d+)\) (\d+)-(\d+)/)
{
    $s = $2 . $3 . $4;
}

```

Is there a comparably succinct way to do that in Python, or will I have to do three splits and remove some punctuation explicitly?

Ignore the fact that I’m not validating the number of digits; that just really distracts from the basic question.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [February 14, 2008, 11:59pm UTC](https://boards.straightdope.com/t/string-processing-in-python/437655/4 "2008-02-14T23:59:15Z")

</div>

Python has pretty good support for regexes (though they don’t have every feature that Perl’s do.) To use one, you create and compile a regex object, then call its ‘match’ method against a string. You can then extract the capture groups from the resulting match object.

```auto

import re

pat = re.compile( '\((\d+)\) (\d+)-(\d+)' )

capt = pat.match( '(123) 555-1234' )

print capt.group( 1, 2, 3 )

```

(Note: the above is untested as I don’t have Python installed here at work. ☹ Hopefully it’s enough to get you started. There’s a pretty good tutorial on Python regexes [here](http://www.amk.ca/python/howto/regex/regex.html). )

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [February 15, 2008, 12:03am UTC](https://boards.straightdope.com/t/string-processing-in-python/437655/5 "2008-02-15T00:03:58Z")

</div>

Beaten to it, and **friedo** ’s code works. Note that re has only been standard since Python 1.5.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 15, 2008, 12:45am UTC](https://boards.straightdope.com/t/string-processing-in-python/437655/6 "2008-02-15T00:45:51Z")

</div>

Thanks guys.
