MD3 Working with strings: String operations Flashcards

1
Q

SEE VID CPOURSE 7 MD3

file:///Users/rikkpaul/Desktop/Cyber%20Security/VIDEO%20LINKS/COURSE%207%20MD3/index.webm

A

Knowing how to work with the string data in security is important. For example, you might find yourself working with usernames to find patterns in login information. We’re going to revisit the string data type and learn how to work with it in Python.

First, let’s have a quick refresher on the strings. We defined the string data as data consisting of an ordered sequence of characters. In Python, strings are written in between quotation marks. It’s okay to use either double or single quotation marks, but in this course, we’ve been using double quotation marks. As examples, we have the strings “Hello”, “123”, and “Number 1!”

We also previously covered variables. Here, the variable my_string is currently storing the string “security”.

ou can also create a string from another data type, such as an integer or a float. To do that, we need to introduce a new built-in function, the string function. The string function is a function that converts the input object into a string. Converting objects to strings allows us to perform tasks that are only possible for strings. For example, we might convert an integer into a string to remove elements from it or to re-order it. Both are difficult for an integer data type.

Let’s practice converting an integer to a string. We’ll apply the string function to the integer 123. Now, the variable new_string contains a string of three characters: 1, 2, and 3. Let’s print its type to check. We’ll run it. Perfect, it tells us that we now have a string! Awesome! So far, we know different ways to create and store a string. Now, let’s explore how to perform some basic string operations.

Our first example is the length function. The length function is a function that returns the number of elements in an object. Using it on a string tells us how many characters the string has. Earlier in the program, we learned that IP addresses have two versions, IPv4 or IPv6. IPv4 addresses have a maximum of 15 characters. So a security professional might use the length function to check if an IPv4 address is valid. If its length is greater than 15 characters, then we’d know that it’s an invalid IPv4 address.

Let’s use this function to print the length of the string “Hello”

We’ll nest the length function within the print function because we want to first calculate the length of this string and then print it to the screen. Okay, let’s run this and check out how many characters Python counts. The output is 5, one for each letter in the word Hello.

We can also use the addition operator on the strings. This is called string concatenation. The string concatenation is the process of joining two strings together. For example, we can add the strings “Hello” and “world” together. To concatenate strings, we can use the + symbol. After we run it, we get “Helloworld” with no spaces in between the two strings. It’s important to note that some operators don’t work for strings. For example, you cannot use a minus sign to subtract the two strings.

Finally, we’re going to talk about string methods. A method is a function that belongs to a specific data type. So, using a string method on another data type, like an integer, would cause an error. Unlike other functions, methods appear after the string. Two common string methods are the upper and the lower methods. The upper method returns a copy of the string in all uppercase letters.

Let’s apply the upper method to the string “Hello”

We’ll place this inside of a print function to output it to the screen. Let’s focus on the unique syntax of methods. After our string “Hello”, we place a period or dot, and then specify the method we want to use. Here, that’s upper()

Okay, now we’re ready to run this. HELLO is printed to the screen in all uppercase letters.

Similarly, the lower method returns a copy of the string in all lowercase letters. Let’s apply the lower method on the “Hello” string. Remember that we need to put the string and the method inside of a print function to output the results. And now, we have the string printed in all lowercase letters.

Coming up, we’re going to learn a lot more about strings, like indexing and splitting strings. I’m looking forward to meeting you there!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly