Procedures: Built-in Functions and String Methods#

Strings, as is true of all objects in Python, come with methods or procedures of various kinds that operate on the string itself.

Procedures#

Before we go too far, it’s important to understand what procedures do.

See also

A procedure is a description of a process. A simple process can be described just by listing the steps. The list of steps is the procedure; the act of following them is the process. A procedure that can be followed without any thought is called a mechanical procedure.

[Evans, 2011]

Methods and functions are two types of procedures. The distinction is not important right now — for our immediate intents and purposes, they operate the same way. Procedure is the more generic term and let’s use that for now.

Built In Procedures in Python#

In the next part, we’ll build our own procedures. First, let’s note just a small sample of procedures Python itself supplies.

You’ve seen at least two Python procedures:

  • print()

  • str()

The print() procedure does just that. However, str() takes in an integer and returns a string.

Now think about that.

The print() procedure returns nothing (or None being the keyword in Python); it simply carries out the procedure of printing to the console and stops.

But str() returns something, i.e., a value that can be used immediately or later on.

That means you can assign a variable to the return value.

For example:

num = 200
# store the string '200' in variable num_converted
num_converted = str(num)
# value of variable 'num_converted'
print(f'Value of num_converted is {num_converted}')
# type of num_converted
print(f'But num_coverted is not an integer; rather it is of type {type(num_converted)}')
Value of num_converted is 200
But num_coverted is not an integer; rather it is of type <class 'str'>

find () is a Method All Strings Carry#

The find() method is a procedure with which all strings come equipped.

Importantly, find() returns a value.

find() is used to find the starting index value of a sub-string within the string itself. The return value or output of find() is the position of the string where the specified sub-string is found, i.e., the return value is an integer.

For example, if you have the string:

common_sense = "These are the times that try men's souls."

and you want to find the first appeareance of the of the substring times, you can retrieve the index value of that substring’s start using find() this way:

common_sense = "These are the times that try men's souls."

the_times = common_sense.find('times')

print(the_times)
14

common_sense.find('times') returns the value, an integer value, 14.

That means that the substring times begins at index 14 in the string common_sense.

Another example, from the world of DNA sequencing.

DNA sequencing determines the nucleic acid sequence or order of nucleotides in DNA. In mammalian DNA, the four nucleotide bases are adenine, guanine, cytosine, and thymine — or A, G, C, T by conventional abbreviation.

Thus, were you to have a DNA sequence which reads, at the start

dna = 'ATGCTCGAAT ...'

and you want to find where in the sequence the sub-sequence GCT is found.

index_of_gtc = dna.find('GCT')

would return, in our example string, 2:

dna = 'ATGCTCGAAT ...'
index_of_gtc = dna.find('GCT')

print(index_of_gtc)
2

You can visualize how find() operates below. It begins at index value 0 and iterates over the sequence. When it finds G it records it. If C is the next value, it keeps track of the indexes [2, 3]. (Were G followed immediately by any other character/nucleotide, the list begun would be cleared out and begin the search on index value 4).

But once find() uncovers (GCT) in order, it returns the index value of the start of that sub-sequence [2,3,4].

DNA

It is essential that you try these things out for yourself. You cannot absolutely cannot learn to code without getting your hands dirty.