dreamsys software

Python Scripting Tutorial

String Manipulation (trim, substring, etc)


One of the powerful features of Python is its ease of use in string manipulation. Operations such as substring, split, trim and other common string manipulations are easy under Python.

Here we will give a simple example that shows many of these operations.

#!/usr/bin/env python

strUsers = 'rpulley  ,    jsmith, svai,  jsatriani    ,ymalmsteen    '
arrUsers = strUsers.split(',')

for user in arrUsers:
    trimUser = user.strip()
    trimUserR = user.rstrip()
    trimUserL = user.lstrip()

    firstInitial = trimUser[:1]
    lastInitial = trimUser[1:2]
    lastName = trimUser[1:]

    print 'User : \'' + user + '\''
    print 'LTrim: \'' + trimUserL + '\''
    print 'RTrim: \'' + trimUserR + '\''
    print ' Trim: \'' + trimUser + '\''

    print 'First Initial:', firstInitial.upper()
    print 'Last Initial: ', lastInitial.upper()
    print 'Last Name:', lastName

    print ''

So in the first line, we have a simple string, which has a list of User IDs separated by commas. These user IDs are in the format first initial followed by last name, there is also some noise in the form of spaces around some of the names. This happens very often in programming situations, especially when dealing with databases. The common way to deal with this is to "trim" the strings. In Python trims are referred to as stripping, which strips the whitespace from the string. The first thing we need to do, however, is to first get this string into an array, so we use the string split function to split the string into several substrings based on a given token, which we give the ',' as a parameter for.

Now we have an array of user strings in the variable arrUsers. Now that we have this array, we can simply use a for loop to iterate through all of the users. The first thing we need to do with each user is get rid of the whitespace noise around the user ID. This is done with Python's string strip function. This function will strip any whitespace off the start and end of the string. If you only want to strip whitespace off the beginning of a string, you can use lstrip (left strip). Likewise for stripping off of just the end of the string you can use rstrip (right strip).

Now that we have the user ID cleaned of the whitespace, we will want to substring to get the first initial, last initial and last name from the user ID. Python doesn't have an exact string substring function, instead you will use the index operator []. Inside of the brackets, you will put the start and end position to substring separated by a colon :.

In this example we also show that if you leave out the start pos, it will by default use zero and if you leave out the end pos, it will by default use the end of the string. When we print the first and last initials, we want these letters to be capitalized, so we use Python's string upper function to convert the python string to uppercase. Likewise there is a lower function if you want to convert a python string to lower case.

There are some essential data structures which are supported in Python, such as Lists and Maps, which we will cover in the next section.

Prev (Classes) | Next (Lists & Maps)

Support Boy Scouts

Buy Popcorn



Blog Entries
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6