Course sections

Getting Started with Python Programming, Lecture 6

String functions

Python has several built-in functions associated with the string data type. These functions let us easily modify and manipulate strings. We can think of functions as being actions that we perform on elements of our code. Built-in functions are those that are defined in the Python programming language and are readily available for us to use.

Making Strings Upper and Lower Case
The functions str.upper() and str.lower() will return a string with all the letters of an original string converted to upper- or lower-case letters. Because strings are immutable data types, the returned string will be a new string. Any characters in the string that are not letters will not be changed.
Let’s convert the string Sammy Shark to be all upper case:
ss = “Sammy Shark”
Now, let’s convert the string to be all lower case:
sammy shark
The str.upper() and str.lower() functions make it easier to evaluate and compare strings by making case consistent throughout. That way if a user writes their name all lower case, we can still determine whether their name is in our database by checking it against an all upper-case version, for example.

Boolean Methods
Python has some string methods that will evaluate to a Boolean value. These methods are useful when we are creating forms for users to fill in, for example. If we are asking for a post code we will only want to accept a numeric string, but when we are asking for a name, we will only want to accept an alphabetic string.
There are a number of string methods that will return Boolean values:

Let’s look at a couple of these in action:
number = “5”
letters = “abcdef”

Similarly, we can query whether a string’s alphabetic characters are in title case, upper case, or lower case. Let’s create a few strings:
movie = “2001: A SAMMY ODYSSEY”
book = “A Thousand Splendid Sharks”
poem = “sammy lived in a pretty how town”
Now let’s try the Boolean methods that check for case:
Now we can run these small programs and see the output:
Output of movie string
Output of book string
Output of poem string

Determining String Length
The string method len() returns the number of characters in a string. This method is useful for when you need to enforce minimum or maximum password lengths, for example, or to truncate larger strings to be within certain limits for use as abbreviations.
To demonstrate this method, we’ll find the length of a sentence-long string:
open_source = “Sammy contributes to open source.”

join(), split(), and replace() Methods

The str.join(), str.split(), and str.replace() methods are a few additional ways to manipulate strings in Python.
The str.join() method will concatenate two strings, but in a way that passes one string through another.
Let’s create a string:
balloon = “Sammy has a balloon.”
Now, let’s use the str.join() method to add whitespace to that string, which we can do like so:
” “.join(balloon)
If we print this out:
print(” “.join(balloon))
We will see that in the new string that is returned there is added space throughout the first string:
S a m m y h a s a b a l l o o n .

We can also use the str.join() method to return a string that is a reversal from the original string:
.noollab a sah ymmaS

We did not want to add any part of another string to the first string, so we kept the quotation marks touching with no space in between. The str.join() method is also useful to combine a list of strings into a new single string.
Let’s create a comma-separated string from a list of strings:
print(“,”.join([“sharks”, “crustaceans”, “plankton”]))

If we want to add a comma and a space between string values in our new string, we can simply rewrite our expression with a whitespace after the comma: “, “.join([“sharks”, “crustaceans”, “plankton”]).
Just as we can join strings together, we can also split strings up. To do this, we will use the str.split()method:
[‘Sammy’, ‘has’, ‘a’, ‘balloon.’]

The str.split() method returns a list of strings that are separated by whitespace if no other parameter is given.
We can also use str.split() to remove certain parts of an original string. For example, let’s remove the letter a from the string:
[‘S’, ‘mmy h’, ‘s ‘, ‘ b’, ‘lloon.’]

Now the letter a has been removed and the strings have been separated where each instance of the letter a had been, with whitespace retained. The str.replace() method can take an original string and return an updated string with some replacement. Let’s say that the balloon that Sammy had is lost. Since Sammy no longer has this balloon, we will change the substring “has” from the original string balloon to “had” in a new string:
print(balloon.replace(“has”,”had”)). Within the parentheses, the first substring is what we want to be replaced, and the second substring is what we are replacing that first substring with. Our output will look like this:
Sammy had a balloon.
Using the string methods str.join(), str.split(), and str.replace() will provide you with greater control to manipulate strings in Python.

WhatsApp chat