Tutorial:Jython/5 String processing

From HandWiki


Here we take a look at the various methods of manipulating strings, covering things from basic methods to regular expressions in Python.

String Methods

The most basic way to manipulate strings is through the methods that are build into them. We can perform a limited number of tasks to strings through these methods. Open up the Python interactive interpreter. Let's create a string: <jc lang="python"> test = 'My first string' print test </jc> Let us count the number of characters in this string, i.e. we want to calculate its length. Use the function len: <jc lang="python"> test = 'My first string' print len ( test ) </jc> Let's take our string and replace a word using the replace method:

<jc lang="python"> test = 'My first string' test = test.replace ( 'first', 'second' ) print test </jc>

Now let's count the number of times a given word (or a character) appears in a string:

<jc lang="python"> test = 'My first string' print test.count( 'r' ) </jc>

To find a character or words, use "find". It finds the first occurrence of a given character or a word:

<jc python edit> test = 'My first string' print test.find( 's' ) </jc>

How to extract one charter from a string? Use [] brackets:

<jc lang="python"> test = 'My first string' print test [ 5 ] </jc>

String Manipulation

Very often one needs to split a string on pieces. Use the split() method for this:

<jc lang="python"> test='My new sentence' a=test.split() print a </jc> It assumes that a single space is the point to split. One can choose the point that we split it at:

<jc lang="python"> test='My new sentence' print test.split ( 'e' ) </jc>

Rejoining our split string can be done using the join method:

<jc lang="python"> test='My new sentence' a=test.split() print .join (a) </jc>

We can play around with the case of letters in our string, too. Let's make it all upper case: <jc lang="python"> test='My new sentence' a=test.split() print test.upper() print test.lower() </jc>

One capitalize only the first letter of the lowercase string:

<jc lang="python"> test='mY new sentence' print test.lower().capitalize() </jc>

We can also use the title method. This capitalizes the first letter in each word:

<jc lang="python"> test='my new sentence' print test.title() </jc>

Trading case is possible:

<jc lang="python"> test='my neW sentence' print test.swapcase() </jc>

We can run a number of tests on strings using a few methods. Let's check to see whether a given string is all upper case:

<jc lang="python"> print 'UPPER'.isupper() print 'UpPEr'.isupper() </jc>

Likewise, we can check to see whether a string contains only lower case characters:

<jc lang="python"> print 'lower'.islower() print 'Lower'.islower() </jc> Checking whether a string looks like a title is simple, too:

<jc lang="python"> print 'This Is A Title'.istitle() </jc>

Numbers and spaces

Now can check whether a string is alphanumeric:

<jc lang="python"> print 'aa22'.isalnum() print 'a$11'.isalnum() </jc>

It is also possible to check whether a string contains only letters:

<jc lang="python"> print 'letters'.isalpha() print 'letters4'.isalpha() </jc>

Here's how you check whether a string contains only numbers:

<jc lang="python"> print '3120'.isdigit() print '20-10-91 Triangle'.isdigit() </jc>

We can also check whether a string only contains spaces:

<jc lang="python"> print ' '.isspace() print .isspace() </jc>

Speaking of spaces, we can add spaces on either side of a string. Let's add spaces to the right of a string:

<jc lang="python"> 'A string.'.ljust ( 15 ) 'A string. ' </jc>

To add spaces to the left of a string, the rjust method is used:

<jc lang="python"> 'A string.'.rjust ( 15 ) ' A string.' </jc> The center method is used to center a string in spaces:

<jc lang="python"> 'A string.'.center ( 15 ) ' A string. ' </jc>

We can strip spaces on either side of a string:

<jc lang="python"> 'String.'.rjust ( 15 ).strip() 'String.' 'String.'.ljust ( 15 ).rstrip() 'String.' </jc>


Regular Expressions

Regular expressions allow patterns to be matched against strings. Actions such as replacement can be performed on the string if the regular expression pattern matches. Python's module for regular expressions is the re module. Open the Python interactive interpreter, and let's take a closer look at regular expressions and the re module:

<jc lang="python"> import re </jc> Let's create a simple string we can use to play around with:

<jc lang="python"> test = 'This is for testing regular expressions in Python.' </jc>

I spoke of matching special patterns with regular expressions, but let's start with matching a simple string just to get used to regular expressions. There are two methods for matching patterns in strings in the re module: search and match. Let's take a look at search first. It works like this:

<jc lang="python"> result = re.search ( 'This', test ) </jc>

We can extract the results using the group method:


<jc lang="python"> result.group ( 0 ) 'This' </jc>

You're probably wondering about the group method right now and why we pass zero to it. It's simple, and I'll explain. You see, patterns can be organized into groups, like this:

<jc lang="python"> result = re.search ( '(Th)(is)', test ) </jc>

There are two groups surrounded by parenthesis. We can extract them using the group method:

<jc lang="python"> result.group ( 1 ) 'Th' result.group ( 2 ) 'is' </jc>

Passing zero to the method returns both of the groups:

<jc lang="python"> result.group ( 0 ) 'This' </jc>

The benefit of groups will become more clear once we work our way into actual patterns. First, though, let's take a look at the match function. It works similarly, but there is a crucial difference:

<jc lang="python"> result = re.match ( 'This', test ) print result <_sre.sre_match> print result.group ( 0 ) 'This' result = re.match ( 'regular', test ) print result None </jc>

Notice that None was returned, even though “regular” is in the string. If you haven't figured it out, the match method matches patterns at the beginning of the string, and the search function examines the whole string. You might be wondering if it's possible, then, to make the match method match “regular,” since it's not at the beginning of the string. The answer is yes. It's possible to match it, and that brings us into patterns.

The character “.” will match any character. We can get the match method to match “regular” by putting a period for every letter before it. Let's split this up into two groups as well. One will contain the periods, and one will contain “regular”:

<jc lang="python"> result = re.match ( '(....................)(regular)', test ) result.group ( 0 ) 'This is for testing regular' result.group ( 1 ) 'This is for testing ' result.group ( 2 ) 'regular' </jc>

We matched it! However, it's ridiculous to have to type in all those periods. The good news is that we don't have to do that. Take a look at this and remember that there are twenty characters before “regular”:

<jc lang="python"> result = re.match ( '(.{20})(regular)', test ) result.group ( 0 ) 'This is for testing regular' result.group ( 1 ) 'This is for testing ' result.group ( 2 ) 'regular' </jc>

That's a lot easier. Now let's look at a few more patterns. Here's how you can use brackets in a more advanced way:

<jc lang="python"> result = re.match ( '(.{10,20})(regular)', test ) result.group ( 0 ) 'This is for testing regular' result = re.match ( '(.{10,20})(testing)', test ) 'This is for testing' </jc>

By entering two arguments, so to speak, you can match any number of characters in a range. In this case, that range is 10-20. Sometimes, however, this can cause undesired behavior. Take a look at this string:

<jc lang="python"> anotherTest = 'a cat, a dog, a goat, a person' </jc>

Let's match a range of characters:

<jc lang="python"> result = re.match ( '(.{5,20})(,)', anotherTest ) result.group ( 1 ) 'a cat, a dog, a goat' </jc>

What if we only want “a cat” though? This can be done with appending “?” to the end of the brackets:

<jc lang="python"> result = re.match ( '(.{5,20}?)(,)', anotherTest ) result.group ( 1 ) 'a cat' </jc>

Appending a question mark to something makes it match as few characters as possible. A question mark that does that, though, is not to be confused with this pattern:

<jc lang="python"> anotherTest = '012345' result = re.match ( '01?', anotherTest ) result.group ( 0 ) '01' result = re.match ( '0123456?', anotherTest ) result.group ( 0 ) '012345' </jc> As you can see with the example, the character before a question mark is optional. Next is the “*” pattern. It matches one or more of the characters it follows, like this:

<jc lang="python"> anotherTest = 'Just a silly string.' result = re.match ( '(.*)(a)(.*)(string)', anotherTest ) result.group ( 0 ) 'Just a silly string' </jc>

However, take a look at this:

<jc lang="python"> anotherTest = 'Just a silly string. A very silly string.' result = re.match ( '(.*)(a)(.*)(string)', anotherTest ) result.group ( 0 ) 'Just a silly string. A very silly string' </jc>

What if, however, we want to only match the first sentence? If you've been following along closely, you'll know that “?” will, again, do the trick:

<jc lang="python"> result = re.match ( '(.*?)(a)(.*?)(string)', anotherTest ) result.group ( 0 ) 'Just a silly string' </jc>

As I mentioned earlier, though, “*” doesn't have to match anything:

<jc lang="python"> result = re.match ( '(.*?)(01)', anotherTest ) result.group ( 0 ) '01' </jc>

What if we want to skip past the first two characters? This is possible by using “+”, which is similar to “*”, except that it matches at least one character:

<jc lang="python"> result = re.match ( '(.+?)(01)', anotherTest ) result.group ( 0 ) '0101' </jc>

We can also match a range of characters. For example, we can match only the first four letters of the alphabet:

<jc lang="python"> anotherTest = 'a101' result = re.match ( '[a-d]', anotherTest ) print result <_sre.sre_match> anotherTest = 'q101' result = re.match ( '[a-d]', anotherTest ) print result None </jc>

We can also match one of a few patterns using “|”::


<jc lang="python"> testA = 'a' testB = 'b' result = re.match ( '(a|b)', testA ) print result <_sre.sre_match> result = re.match ( '(a|b)', testB ) print result <_sre.sre_match> </jc>

Finally, there are a number of special sequences. “\A” matches at the start of a string. “\Z” matches at the end of a string. “\d” matches a digit. “\D” matches anything but a digit. “\s” matches whitespace. “\S” matches anything but whitespace.

We can name our groups:

<jc lang="python"> nameTest = 'hot sauce' result = re.match ( '(?Phot)', nameTest ) result.group ( 'one' ) 'hot' </jc>

We can compile patterns to use them multiple times with the re module, too:


<jc lang="python"> ourPattern = re.compile ( '(.*?)(the)' ) testString = 'This is the dog and the cat.' result = ourPattern.match ( testString ) result.group ( 0 ) 'This is the' </jc>

Of course, you can do more than match and extract substrings. You can replace things, too:

<jc lang="python"> someString = 'I have a dream.' re.sub ( 'dream', 'dog', someString ) 'I have a dog.' </jc>

On a final note, you should not use regular expressions to match or replace simple strings.

Conclusion

Now you have a basic knowledge of string manipulation in Python behind you. As I explained at the very beginning of the article, string manipulation is necessary to many applications, both large and small. It is used frequently, and a basic knowledge of it is critical.