Python Strings

 

What are they?

A string is a special collection of character data

Here is an example of a string constant:

"Monday"

You can store a string in a variable like this:

>>> day = "Monday"

Remember, in Python variable takes on the type of the assigned data

You can look at the value of a character in a string by specifying its �index�

The first character in the string is at position 0

Subsequent characters are incremented to the right

The final character is at an index value equal to the length of the string minus 1

So, for the string value "Monday":

Value

M

o

n

d

a

y

Index

0

1

2

3

4

5

 

Getting inside strings

You can find the character at index 3 of "Monday" like this:

>>> "Monday"[3]

'd'

You can also use this technique with variable names:

>>> day = "Monday"

>>> day[3]

'd'

Python has a lot of great built in tools for working with strings

It is often handy to be able to �parse� them

To extract and use portions of a string

We often �read� data from files as sets of strings

Here is a string representing a UTM coordinate triple:

>>> positionAsString = "18 N 572346 4723109422"

Suppose that the file has a �fixed format� such that on a given line:

Digits 0-1 represent the UTM zone

Digits 2-3 represent the UTM hemisphere (S or N)

Digits 4-10 represent the position easting

Digits 11-18 represent the position northing and

Digits 19-23 represent the position elevation

Suppose we want to store portions of the string as numeric values

The following line of code lets us store a portion of the string (a substring) as the variable easting:

>>> eastingAsString = positionAsString[4:11]

Note: define the starting point of the substring with an index value equal to the first character of the substring

The ending value (11 here) is always 1 greater than the index of the last substring character (10)

Now eastingAsString holds " 572346"

This is a string representation consisting of 7 characters (the first is a �space� character)

StringPractice.py demonstrates a few of these concepts

Look here for lots of information on strings and reading from files

Converting strings to other types

How can we convert this to a numeric value? Like this:

>>> eastingAsFloat = float(eastingAsString)

And now, eastingAsFloat holds 572346.0

We need to convert strings to numeric representations before we can perform any kind of mathematical operations on them

Try this:

>>> twoAsString = "2"

>>> twoAsString * 2

Not an error, but maybe not what you were expecting!

Now try this:

>>> twoAsInt = int(twoAsString)

>>> twoAsInt * 2

Much better!

 

Handy string functions and techniques

More on substrings

We have already seen how to extract a portion of a string

The complete �slicing� syntax looks like this:

myString[start:stop:step]

where start is the beginning index, stop is the ending index + 1, and step is an integer that can allow you to move forward or backwards in the string (possibly in jumps larger than 1)

If you leave out a value, it is replaced with a default value

Try these out:

To print the string in reverse:

>>> "Monday"[::-1]

To print every other character, moving forward:

>>> "Monday"[::2]

The possibilities are endless

Some generic functions work on strings as well as other types (like lists�coming to a lecture near you soon!)

len()

Returns the length of a string

>>> len("Monday")

6

Other functions are specific to strings and use the �dot� notation

If you know Java, this notation works similarly in Python

If you don�t, we�ll look at this in detail later

.upper()

>>> "Monday".upper()

MONDAY

.lower()

>>> "Monday".lower()

monday

We will run into many other string functions as we need them