Working with strings in Python

Coding with Python

🕑 This lesson will take about 20 minutes

In the past few lessons, you have been working with strings quite a lot. Before we move on, heres a quick refresher. A string is a sequence of characters. A string may be some simple text such as someone’s name, an address, a username or email address, or it could be symbols or numbers. A string is always defined within quotation marks eg "hello world".

Escape characters

When you want to literally use quotation marks or apostrophes inside a string, you might run into issues depending on whether you’re using double or single quotation marks. For example, print("My friend said "hello"") or print('This is Joe's program') will both result in an error because you are placing quotation marks between your quotation marks that contain the string, which confuses Python (basically, once Python sees the second quotation mark in the string value, it will assume that is the end of the string and won’t understand the characters that follow it. This would usually result in a SyntaxError.

Luckily, there are ways of fixing this problem. The first solution for this problem is called an escape character (also known as escape sequence). In Python and many other programming languages, the escape character is a backslash ( \ ). If you are placing your string between double quotation marks then you need to put a backlash before any double quotation mark you actually want to include inside your string. The same applies for when you might want to include an apostrophe or single quotation mark inside a string value that is contained between single quotation marks. In this case, you would need to put a backslash before any single quotation mark you actually want to include inside your string. Check out the example below.

An escape character basically tells Python to ignore the quotation mark immediately following the escape character and to not treat it as an instruction but to use the following quotation mark in the actual string. Python will treat the first quotation mark it sees as the beginning of your string and the last quotation mark it sees as the end of your string. All other quotation marks inside the string need to be “escaped” (have an escape character before them). An alternative solution, if you have a single quote inside a string, is to surround the string by double quotes; or if you will have a double quote inside a string, then surround that string by single quotes.

You can also contain a string inside three single quotes and use double/single quotes inside the string. For example:

String concatenation

Earlier in the course, we looked at how to concatenate (join) strings, for example using the + operator or using f-strings. Here is another method using the %s token.

The ‘findʼmethod

There is a way of finding where a word or value begins in a string. Let’s look at the example below. We have a variable called sentence which contains a string. We want to know where exactly in that string the word ‘youʼ begins. To do this we first identify the object (which is the sentence variable), call the method (which is .find) and specify a value for the parameter (which is “you”). Typing in sentence.find("you") will return the value of 8 (where 8 is the index of where the substring “you” begins in the string “How are you today?”). The index refers to the position of a character within a string. Indexing begins at 0, so that means the first character has an index of 0, the second character has an index of 1, the third character has an index of 2, and so on. In this example, the first letter of the word “you” is in the 9th position in the larger string, so the find method returns the index of 8. Keep in mind that spaces are counted as characters in a string too.

Converting a string to lower case or upper case

A common task when working with strings is converting to lower case or upper case. We can use the upper() and lower() methods to convert string variables to upper or lower case.

In the example below, we have a string variable containing an email address. Unfortunately, a user has typed their email address with mixed upper and lower case letters (“jOeSmiTh@gMAiL.cOm”). We might want to convert all email addresses to lowercase for consistency. Here is an example of how that can be achieved:

And in this example, we will convert letters in a string to upper case letters.

Check if a string is upper- or lowercase letters only

You can also check if a string is uppercase or lowercase letter(s) only using the isupper() and islower() functions, as shown in the example below.

The replace method

Let’s say you have a string such as “I like the xbox” and you want to replace the word “xbox” with the superior console, “playstation” 😜😜. We can use the replace method to do this by telling Python the old word we want to remove and the new word we want to replace it with. In the example below, the parameters provided to the replace method are the old word which is “xbox” and the new word which is “playstation” separated by a comma. The replace method is being used on the variable sentence, so we type in sentence.replace("xbox", "playstation") like below. Note that if there are multiple instances of the specified substring, all instances of the specified substring within the string will be replaced with the new specified value.

Find the length of a string

The len() function is used to return the number of items in an object. When the object is a list, the len() function will return the number of elements in that list. When the object is a string, the len() function will return the number of characters in that string.

For example, len("cat") will return 3.

Check if string contains letters only

You can use the isalpha() function to check if a string contains only letters. The isalpha() function returns True if all the characters in the string are alphabet letters (a-z) and False if any characters in the string are not alphabet letters, for example, numbers, space, !#%&?, etc.