Get startedGet started for free

Strings

1. Strings

Strings are also a sequence type and likely the most common type you'll encounter. We can loop over them like all the other sequence types we've encountered so far; however, they also have some unique ways we can construct and work with them.

2. Creating formatted strings

We will focus first on the f-string, which is short for formatted string literals. Python has several different string types indicated by a letter in front of the opening quote of the string. To make an f-string, you place an f in front of the opening quote, and then you can use python expressions wrapped in curly braces inside them to access additional data points and incorporate them into the string. For example, if I had two variables named cookie_name set to Anzac and a cookie_price set to one dollar ninety-nine, I could use them to print out the string you see here. f" Each { cookie_name } cookie costs { cookie_price }", which would output: "Each Anzac cookie costs one dollar ninety-nine".

3. Joining with strings

Another helpful feature of strings is that they support the use of a join() method on them that you can use the string to join some other iterable data type like a list. For example, if we have a list of child_ages containing "3", "4", "7", and "8". We can join them with a comma by doing the following. ', ', join(child_ages), which would output "3, 4, 7, 8". We could combine this with an f-string to do even more. f"The children are ages {','join(child_ages[0:3])}, and {child_ages[-1]}". Which would output: "The children are ages 3, 4, 7, and 8".

4. Matching parts of a string

Now that we've explored just a few ways to create strings let's look at how we can find things inside strings. One approach to finding strings that start or end with a specific letter is to use the startswith() or endswith() methods. For example, if we have a list of boy_names Mohamed, Youssef, and Ahmed, we can find the one that starts with 'A' by using a list comprehension. Note these functions are case-sensitive. For example: [name for name in boy_names where name-dot-startswith('A')] would return: ['Ahmed'].

5. Searching for things in strings

Another way to find things in a string is to use the in operator. This searches for some value inside a string. For example, we can look for the word long in this quote by doing the following: "long" in "Life is a long lesson in humility." True "life" in "Life is a long lesson in humility." False This is due to this search being case sensitive and Life being capitalized in the quote but not in our search string.

6. An approach to being case insensitive

A common way to deal with the case sensitivity is to use the lower() method on a string. Let's repeat that last example but lower the quote string. "life" in "Life is a long lesson in humility"-dot-lower() True

7. Let's practice!

Now, let's get caught up in strings like a curious cat.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.