You are a guest. Restricted access. Read more

String processing

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:


test = 'My first string'
print test

Let us count the number of characters in this string, i.e. we want to calculate its length. Use the function len:


test = 'My first string'
print len ( test )

Let's take our string and replace a word using the replace method:


test = 'My first string'
test = test.replace ( 'first', 'second' )
print test

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


test = 'My first string'
print test.count( 'r' )

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


test = 'My first string'
print test.find( 's' )

Exercise: Replace the character “s” in the above example by “ng” and print the answer. Try other combinations as well

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


test = 'My first string'
print test [ 5 ]

String Manipulation

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


test='My new sentence'
a=test.split()
print a

It assumes that a single space is the point to split. One can choose the point that we split it at:


test='My new sentence'
print test.split ( 'e' )

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


test='My new sentence'
a=test.split()
print ''.join (a)

We can play around with the case of letters in our string, too. Let's make it all upper case:


test='My new sentence'
a=test.split()
print test.upper()
print test.lower()

One capitalize only the first letter of the lowercase string:


test='mY new sentence'
print test.lower().capitalize()

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


test='my new sentence'
print test.title()

Trading case is possible:


test='my neW sentence'
print test.swapcase()

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:


print 'UPPER'.isupper()
print 'UpPEr'.isupper()

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


print 'lower'.islower()
print 'Lower'.islower()

Checking whether a string looks like a title is simple, too:


print 'This Is A Title'.istitle()

Numbers and spaces

Now can check whether a string is alphanumeric:


print 'aa22'.isalnum()
print 'a$11'.isalnum()

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


print 'letters'.isalpha()
print 'letters4'.isalpha()

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


print '3120'.isdigit()
print '20-10-91 Triangle'.isdigit()

We can also check whether a string only contains spaces:


print ' '.isspace()
print ''.isspace()

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


'A string.'.ljust ( 15 )
'A string. '

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


'A string.'.rjust ( 15 )
' A string.'

The center method is used to center a string in spaces:


'A string.'.center ( 15 )
' A string. '

We can strip spaces on either side of a string:


'String.'.rjust ( 15 ).strip()
'String.'
'String.'.ljust ( 15 ).rstrip()
'String.'

python/strings.txt · Last modified: 2013/04/14 16:52 (external edit)
Back to top
CC Attribution-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0