# module 3

# Welcome to module 3

As a security analyst,  
you'll work with a lot of data.  
Being able to develop solutions for  
managing this data is very important.  
What we're about to learn in  
Python will help you with that.  
Previously, we set  
foundations for what we're going to do in this section.  
We learned all about data types and variables.  
We also covered conditional and iterative statements.  
We learned about building  
functions and even created our own functions.  
Here, we'll build on that in a few different ways.  
First, you'll learn more about  
working with strings and lists.  
We'll expand the ways that you  
can work with these data types,  
including extracting characters from  
strings or items from lists.  
Our next focus is on writing algorithms.  
You'll consider a set of rules that can be applied in  
Python to solve a security-related problem.  
Finally, we'll further expand the ways we can search  
for strings when we explore using regular expressions.  
We're going to have a lot of fun, and you'll  
start writing some really interesting code in Python.  
I can't wait to get started.

# New Page



# String operations

<div class="cds-156 css-1shylkf cds-157" id="bkmrk-knowing-how-to-work-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Knowing how to work with </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="2" data-cue-index="1" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the string data in security is important. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">For example, you might find yourself working with </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="4" data-cue-index="3" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">usernames to find patterns in login information. </span></div><div aria-label="toggle video from current lecture segment" class="rc-Phrase active css-13o25cb" data-cue="5" data-cue-index="4" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We're going to revisit </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the string data type and learn </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="7" data-cue-index="6" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">how to work with it in Python. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-first%2C-let%27s-have-a-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="8" data-cue-index="7" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">First, let's have a quick refresher on the strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="9" data-cue-index="8" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We defined the string data as </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="10" data-cue-index="9" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">data consisting of an ordered sequence of characters. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="11" data-cue-index="10" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">In Python, strings are </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">written in between quotation marks. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="13" data-cue-index="12" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">It's okay to use either double or single quotation marks, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">but in this course, we've been </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">using double quotation marks. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">As examples, we have the strings "Hello", </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="17" data-cue-index="16" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">"123", and "Number 1!" </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--1"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-we-also-previously-c"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="18" data-cue-index="17" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We also previously covered variables. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="19" data-cue-index="18" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Here, the variable my\_string </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="20" data-cue-index="19" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">is currently storing the string "security". </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--2"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-you-can-also-create-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="21" data-cue-index="20" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">You can also create a string from another data type, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="22" data-cue-index="21" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">such as an integer or a float. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">To do that, we need to introduce </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="24" data-cue-index="23" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a new built-in function, the string function. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The string function is a function that </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="26" data-cue-index="25" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">converts the input object into a string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="27" data-cue-index="26" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Converting objects to strings allows us to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">perform tasks that are only possible for strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="29" data-cue-index="28" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">For example, we might convert an integer into </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a string to remove elements from it or to re-order it. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="31" data-cue-index="30" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Both are difficult for an integer data type. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--3"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-let%27s-practice-conve"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's practice converting an integer to a string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We'll apply the string function to the integer 123. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="34" data-cue-index="33" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now, the variable new\_string </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">contains a string of three characters: </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">1, 2, and 3. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's print its type to check. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="38" data-cue-index="37" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We'll run it. Perfect, it tells </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">us that we now have a string! </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="40" data-cue-index="39" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Awesome! So far, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="41" data-cue-index="40" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we know different ways to create and store a string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="42" data-cue-index="41" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now, let's explore how to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="43" data-cue-index="42" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">perform some basic string operations. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--4"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-our-first-example-is"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="44" data-cue-index="43" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Our first example is the length function. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="45" data-cue-index="44" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The length function is a function that </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="46" data-cue-index="45" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">returns the number of elements in an object. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Using it on a string tells </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="48" data-cue-index="47" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">us how many characters the string has. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="49" data-cue-index="48" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Earlier in the program, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we learned that IP addresses have </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="51" data-cue-index="50" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">two versions, IPv4 or IPv6. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="52" data-cue-index="51" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">IPv4 addresses have a maximum of 15 characters. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="53" data-cue-index="52" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">So a security professional might use </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="54" data-cue-index="53" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the length function to check if an IPv4 address is valid. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="55" data-cue-index="54" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">If its length is greater than 15 characters, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">then we'd know that it's an invalid IPv4 address. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--5"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-let%27s-use-this-funct"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="57" data-cue-index="56" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's use this function to print </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="58" data-cue-index="57" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the length of the string "Hello" </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--6"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-we%27ll-nest-the-lengt"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="59" data-cue-index="58" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We'll nest the length function </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="60" data-cue-index="59" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">within the print function because we </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="61" data-cue-index="60" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">want to first calculate the length of </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="62" data-cue-index="61" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">this string and then print it to the screen. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="63" data-cue-index="62" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Okay, let's run this and check out </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="64" data-cue-index="63" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">how many characters Python counts. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="65" data-cue-index="64" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The output is 5, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="66" data-cue-index="65" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">one for each letter in the word Hello. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--7"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-we-can-also-use-the-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="67" data-cue-index="66" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We can also use the addition operator on the strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="68" data-cue-index="67" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">This is called string concatenation. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="69" data-cue-index="68" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The string concatenation is </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="70" data-cue-index="69" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the process of joining two strings together. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="71" data-cue-index="70" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">For example, we can add </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="72" data-cue-index="71" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the strings "Hello" and "world" together. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="73" data-cue-index="72" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">To concatenate strings, we can use the + symbol. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="74" data-cue-index="73" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">After we run it, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="75" data-cue-index="74" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we get "Helloworld" with </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="76" data-cue-index="75" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">no spaces in between the two strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="77" data-cue-index="76" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">It's important to note that </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="78" data-cue-index="77" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">some operators don't work for strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="79" data-cue-index="78" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">For example, you cannot use </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="80" data-cue-index="79" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a minus sign to subtract the two strings. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--8"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-finally%2C-we%27re-going"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="81" data-cue-index="80" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Finally, we're going to talk about string methods. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="82" data-cue-index="81" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">A method is a function </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="83" data-cue-index="82" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">that belongs to a specific data type. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="84" data-cue-index="83" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">So, using a string method on another data type, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="85" data-cue-index="84" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">like an integer, would cause an error. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="86" data-cue-index="85" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Unlike other functions, methods appear after the string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="87" data-cue-index="86" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Two common string methods are </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="88" data-cue-index="87" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the upper and the lower methods. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="89" data-cue-index="88" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The upper method returns a copy of </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="90" data-cue-index="89" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the string in all uppercase letters. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--9"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-let%27s-apply-the-uppe"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="91" data-cue-index="90" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's apply the upper method to the string "Hello" </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--10"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-we%27ll-place-this-ins"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="92" data-cue-index="91" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We'll place this inside of </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="93" data-cue-index="92" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a print function to output it to the screen. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="94" data-cue-index="93" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's focus on the unique syntax of methods. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="95" data-cue-index="94" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">After our string "Hello", </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="96" data-cue-index="95" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we place a period or dot, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="97" data-cue-index="96" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">and then specify the method we want to use. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="98" data-cue-index="97" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Here, that's upper() </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--11"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-okay%2C-now-we%27re-read"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="99" data-cue-index="98" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Okay, now we're ready to run this. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="100" data-cue-index="99" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">HELLO is printed to the screen in all uppercase letters. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--12"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-similarly%2C-the-lower"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="101" data-cue-index="100" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Similarly, the lower method returns </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="102" data-cue-index="101" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a copy of the string in all lowercase letters. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="103" data-cue-index="102" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's apply the lower method on the "Hello" string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="104" data-cue-index="103" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Remember that we need to put the string and the method </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="105" data-cue-index="104" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">inside of a print function to output the results. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="106" data-cue-index="105" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">And now, we have the string </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="107" data-cue-index="106" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">printed in all lowercase letters. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--13"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-coming-up%2C-we%27re-goi"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="108" data-cue-index="107" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Coming up, we're going to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="109" data-cue-index="108" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">learn a lot more about strings, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="110" data-cue-index="109" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">like indexing and splitting strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="111" data-cue-index="110" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">I'm looking forward to meeting you there! </span></div></div>

# String indices and slices

<div class="cds-156 css-1shylkf cds-157" id="bkmrk-in-security%2C-there-a"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">In security, there are a variety </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="2" data-cue-index="1" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">of reasons we might need to search through a string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">For example, we might need to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="4" data-cue-index="3" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">locate a username in a security log. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="5" data-cue-index="4" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Or, if we learn that </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a certain IP address is associated with malware, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="7" data-cue-index="6" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we might search for this address in a network log. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="8" data-cue-index="7" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">And, the first step in being able to use Python in </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="9" data-cue-index="8" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">these ways is learning </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="10" data-cue-index="9" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">about the index of characters in a string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="11" data-cue-index="10" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The index is a number assigned to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">every element in a sequence that indicates its position. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="13" data-cue-index="12" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">In this video, we are discussing strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">So, the index is </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the position of each character in a string. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-let%27s-start-with-the"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's start with the string "HELLO." </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="17" data-cue-index="16" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Every character in the string is assigned an index. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="18" data-cue-index="17" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">In Python, we start counting indices from 0. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="19" data-cue-index="18" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">So, the character "H" has an index of 0, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="20" data-cue-index="19" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">and "E" has an index of 1, and so on. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="21" data-cue-index="20" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's take this into Python and practice using indices. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--1"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-placing-an-index-in-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="22" data-cue-index="21" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Placing an index in square brackets after </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a string returns the character at that index. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="24" data-cue-index="23" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's place the index 1 in </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">square brackets after "HELLO" and run it. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="26" data-cue-index="25" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">This returned the character "E." </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="27" data-cue-index="26" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Remember, indices start at 0, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">so an index of 1 isn't the first character in the word. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--2"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-but-what-if-we-want-"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="29" data-cue-index="28" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">But what if we want it to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">return more than just one character? </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="31" data-cue-index="30" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We can extract a larger part of </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">a string by specifying a set of indices. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">This is called a slice. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="34" data-cue-index="33" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">When taking a slice from a string, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we specify where the slice </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">starts and where the slice ends. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">So we provide two indices. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="38" data-cue-index="37" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The first index is the beginning, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">which is included in the output. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="40" data-cue-index="39" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The second index is the end, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="41" data-cue-index="40" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">but it's not included in the final output. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="42" data-cue-index="41" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Instead, Python stops </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="43" data-cue-index="42" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the slice at the element before the second index. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="44" data-cue-index="43" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">For example, if we wanted to take the letters E-L-L </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="45" data-cue-index="44" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">from "HELLO," </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="46" data-cue-index="45" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we would start the interval from the index 1, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">but we'd end before the index 4. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--3"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-let%27s-try-this-examp"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="48" data-cue-index="47" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's try this example and </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="49" data-cue-index="48" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">extract a slice from a string in Python. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's type in the string and take the slice starting </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="51" data-cue-index="50" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">at index 1 and ending before index 4. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="52" data-cue-index="51" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now, let's run the code and examine the output. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="53" data-cue-index="52" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">There's the slice we wanted. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--4"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-now-that-we-know-how"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="54" data-cue-index="53" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now that we know how to describe </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="55" data-cue-index="54" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the location of a character in a string, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">let's learn how to search in a string. </span></div><div aria-label="toggle video from current lecture segment" class="rc-Phrase active css-13o25cb" data-cue="57" data-cue-index="56" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">To do this, we need to use the index method. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="58" data-cue-index="57" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The index method finds the first occurrence of </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="59" data-cue-index="58" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the input in a string and returns its location. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="60" data-cue-index="59" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's practice using the index method in Python. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--5"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-let%27s-say-we-want-to"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="61" data-cue-index="60" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's say we want to use the index method </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="62" data-cue-index="61" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">to find the character "E" in the string "HELLO." </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="63" data-cue-index="62" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">We'll locate the first instance of </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="64" data-cue-index="63" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the character "E." Let's examine this line in more detail. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="65" data-cue-index="64" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">After writing the string and the index method, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="66" data-cue-index="65" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we use the character we want to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="67" data-cue-index="66" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">find as the argument of the index method. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="68" data-cue-index="67" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Remember, the strings in Python are case-sensitive, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="69" data-cue-index="68" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">so we need to make sure </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="70" data-cue-index="69" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">we use the appropriate case with the index method. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="71" data-cue-index="70" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's run this code now. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="72" data-cue-index="71" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">This returned the number 1. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="73" data-cue-index="72" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">This is because "E" has an index value of 1. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--6"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-now%2C-let%27s-explore-a"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="74" data-cue-index="73" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now, let's explore an example </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="75" data-cue-index="74" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">where a character repeats multiple times in the string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="76" data-cue-index="75" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's try searching for </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="77" data-cue-index="76" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the character "L." We start with similar code as before, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="78" data-cue-index="77" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">passing the argument "L" instead of "E" to the index method. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="79" data-cue-index="78" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now, let's run this code and investigate the result. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="80" data-cue-index="79" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">The result is the index 2. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="81" data-cue-index="80" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">This tells us that the method only </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="82" data-cue-index="81" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">identified the first occurrence of </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="83" data-cue-index="82" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the character "L" and not the second. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="84" data-cue-index="83" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">This is an important detail to </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="85" data-cue-index="84" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">notice when working with the index method. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--7"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-as-a-security-analys"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="86" data-cue-index="85" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">As a security analyst, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="87" data-cue-index="86" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">learning how to work with indices </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="88" data-cue-index="87" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">allows you to find certain parts in a string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="89" data-cue-index="88" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">For example, if you need to find </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="90" data-cue-index="89" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the location of the @ symbol in an email, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="91" data-cue-index="90" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">you can use the index method to find </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="92" data-cue-index="91" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">what you're looking for with one line of code. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--8"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-now-let%27s-turn-our-a"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="93" data-cue-index="92" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now let's turn our attention </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="94" data-cue-index="93" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">to an important property of the strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="95" data-cue-index="94" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Have you ever heard the expression </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="96" data-cue-index="95" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">"some things never change"? </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="97" data-cue-index="96" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">It might be said about </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="98" data-cue-index="97" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">the comfortable feeling you have with a good friend, </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="99" data-cue-index="98" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">even when you haven't seen them for a long time. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="100" data-cue-index="99" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Well, in Python, we can also say this about strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="101" data-cue-index="100" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Strings are immutable. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="102" data-cue-index="101" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">In Python, "immutable" means that it </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="103" data-cue-index="102" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">cannot be changed after it's </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="104" data-cue-index="103" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">created and assigned a value. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="105" data-cue-index="104" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's break this down with an example. </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--9"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk-let%27s-assign-the-str"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item"><div class="phrases"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="106" data-cue-index="105" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Let's assign the string "HELLO" to the variable my\_string. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="107" data-cue-index="106" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Now, if we want to change the character "E" to an "A" </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="108" data-cue-index="107" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">so my\_string has the value "HALLO," </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="109" data-cue-index="108" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">then we might be inclined to use index notation. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="110" data-cue-index="109" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">But here we get an error. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="111" data-cue-index="110" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">My\_string is immutable, so we cannot </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="112" data-cue-index="111" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">make changes like this. And there you have it! </span></div></div></div></div><div class="cds-156 css-1shylkf cds-157" id="bkmrk--10"><div class="cds-156 rc-Paragraph css-1lz62pp cds-158 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-you%27ve-just-learned-"><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="113" data-cue-index="112" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">You've just learned how to index and slice into strings. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="114" data-cue-index="113" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">You've also seen that strings are immutable. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="115" data-cue-index="114" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">You cannot reassign characters </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="116" data-cue-index="115" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">after a string has been defined. </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="117" data-cue-index="116" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">Coming up, we'll learn </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="118" data-cue-index="117" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">about list operations and see that </span></div><div aria-label="play video from" class="rc-Phrase css-13o25cb" data-cue="119" data-cue-index="118" role="button" tabindex="0"><span aria-hidden="true" class="cds-263 cds-Typography-base css-80vnnb cds-265">lists can be changed with index notation. Meet you there. </span></div></div>

# Strings and the security analyst

The ability to work with strings is important in the cybersecurity profession. Previously, you were introduced to several ways to work with strings, including functions and methods. You also learned how to extract elements in strings using bracket notation and indices. This reading reviews these concepts and explains more about using the <var>.index()</var> method. It also highlights examples of string data you might encounter in a security setting.

## String data in a security setting

As an analyst, string data is one of the most common data types you will encounter in Python. **String data** is data consisting of an ordered sequence of characters. It's used to store any type of information you don't need to manipulate mathematically (such as through division or subtraction). In a cybersecurity context, this includes IP addresses, usernames, URLs, and employee IDs.

You'll need to work with these strings in a variety of ways. For example, you might extract certain parts of an IP address, or you might verify whether usernames meet required criteria.

## Working with indices in strings

### Indices 

An **index** is a number assigned to every element in a sequence that indicates its position. With strings, this means each character in the string has its own index.

Indices start at <var>0</var>. For example, you might be working with this string containing a device ID: <var>"h32rb17"</var>. The following table indicates the index for each character in this string:

<div class="rc-CML" dir="auto" id="bkmrk-character-index-h-0-"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="reading_item" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="reading_item"><div class="css-1kgqbsw" data-testid="cml-viewer"><div class="css-1yr0py9"><table><thead><tr><th scope="col">character

</th><th scope="col">index

</th></tr></thead><tbody><tr><td><var>h</var>

</td><td><var>0</var>

</td></tr><tr><td><var>3</var>

</td><td><var>1</var>

</td></tr><tr><td><var>2</var>

</td><td><var>2</var>

</td></tr><tr><td><var>r</var>

</td><td><var>3</var>

</td></tr><tr><td><var>b</var>

</td><td><var>4</var>

</td></tr><tr><td><var>1</var>

</td><td><var>5</var>

</td></tr><tr><td><var>7</var>

</td><td><var>6</var>

</td></tr></tbody></table>

</div></div></div></div></div></div>You can also use negative numbers as indices. This is based on their position relative to the last character in the string:

<div class="rc-CML" dir="auto" id="bkmrk-character-index-h--7"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="reading_item" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="reading_item"><div class="css-1kgqbsw" data-testid="cml-viewer"><div class="css-1yr0py9"><table><thead><tr><th scope="col">character

</th><th scope="col">index

</th></tr></thead><tbody><tr><td><var>h</var>

</td><td><var>-7</var>

</td></tr><tr><td><var>3</var>

</td><td><var>-6</var>

</td></tr><tr><td><var>2</var>

</td><td><var>-5</var>

</td></tr><tr><td><var>r</var>

</td><td><var>-4</var>

</td></tr><tr><td><var>b</var>

</td><td><var>-3</var>

</td></tr><tr><td><var>1</var>

</td><td><var>-2</var>

</td></tr><tr><td><var>7</var>

</td><td><var>-1</var>

</td></tr></tbody></table>

</div></div></div></div></div></div>### Bracket notation

**Bracket notation** refers to the indices placed in square brackets. You can use bracket notation to extract a part of a string. For example, the first character of the device ID might represent a certain characteristic of the device. If you want to extract it, you can use bracket notation for this:

<var>"h32rb17"\[0\]</var>

This device ID might also be stored within a variable called <var>device\_id</var>. You can apply the same bracket notation to the variable:

<var>device\_id = "h32rb17"</var>

<var>device\_id\[0\]</var>

In both cases, bracket notation outputs the character <var>h</var> when this bracket notation is placed inside a <var>print()</var> function. You can observe this by running the following code:

```python
device_id = "h32rb17"
print("h32rb17"[0])
print(device_id[0])
```

```
h
h
```

You can also take a slice from a string. When you take a slice from a string, you extract more than one character from it. It's often done in cybersecurity contexts when you’re only interested in a specific part of a string. For example, this might be certain numbers in an IP address or certain parts of a URL.

In the device ID example, you might need the first three characters to determine a particular quality of the device. To do this, you can take a slice of the string using bracket notation. You can run this line of code to observe that it outputs <var>"h32"</var>:

```python
print("h32rb17"[0:3])
```

```
h32
```

**Note:** The slice starts at the <var>0</var> index, but the second index specified after the colon is excluded. This means the slice ends one position before index <var>3</var>, which is at index <var>2</var>.

## String functions and methods

The <var>str()</var> and <var>len()</var> functions are useful for working with strings. You can also apply methods to strings, including the <var>.upper()</var>, <var>.lower()</var>, and <var>.index()</var> methods. A **method** is a function that belongs to a specific data type.

### str() and len()

The <var>str()</var> function converts its input object into a string. As an analyst, you might use this in security logs when working with numerical IDs that aren't going to be used with mathematical processes. Converting an integer to a string gives you the ability to search through it and extract slices from it.

Consider the example of an employee ID <var>19329302</var> that you need to convert into a string. You can use the following line of code to convert it into a string and store it in a variable:

<var>string\_id = str(19329302)</var>

The second function you learned for strings is the <var>len()</var> function, which returns the number of elements in an object.

As an example, if you want to verify that a certain device ID conforms to a standard of containing seven characters, you can use the <var>len()</var> function and a conditional. When you run the following code, it will print a message if <var>"h32rb17"</var> has seven characters:

```python
device_id_length = len("h32rb17")
if device_id_length == 7:
    print("The device ID has 7 characters.")
```

```
The device ID has 7 characters.
```

### .upper() and .lower() 

The <var>.upper()</var> method returns a copy of the string with all of its characters in uppercase. For example, you can change this department name to all uppercase by running the code <var>"Information Technology".upper()</var>. It would return the string <var>"INFORMATION TECHNOLOGY"</var>.

Meanwhile, the <var>.lower()</var> method returns a copy of the string in all lowercase characters. <var>"Information Technology".lower()</var> would return the string <var>"information technology"</var>.

### .index()

The <var>.index()</var> method finds the first occurrence of the input in a string and returns its location. For example, this code uses the <var>.index()</var> method to find the first occurrence of the character <var>"r"</var> in the device ID <var>"h32rb17"</var>:

```python
print("h32rb17".index("r"))
```

```
3
```

The <var>.index()</var> method returns <var>3</var> because the first occurrence of the character <var>"r"</var> is at index <var>3</var>.

In other cases, the input may not be found. When this happens, Python returns an error. For instance, the code <var>print("h32rb17".index("a"))</var> returns an error because <var>"a"</var> is not in the string <var>"h32rb17"</var>.

Also note that if a string contains more than one instance of a character, only the first one will be returned. For instance, the device ID <var>"r45rt46"</var> contains two instances of <var>"r"</var>. You can run the following code to explore its output:

```python
print("r45rt46".index("r"))
```

```
0
```

The output is <var>0</var> because <var>.index()</var> returns only the first instance of <var>"r"</var>, which is at index <var>0</var>. The instance of <var>"r"</var> at index <var>3</var> is not returned.

### Finding substrings with .index()

A **substring** is a continuous sequence of characters within a string. For example, <var>"llo"</var> is a substring of <var>"hello"</var>.

The <var>.index()</var> method can also be used to find the index of the first occurrence of a substring. It returns the index of the first character in that substring. Consider this example that finds the first instance of the user <var>"tshah"</var> in a string:

```python
tshah_index = "tsnow, tshah, bmoreno - updated".index("tshah")
print(tshah_index)
```

```
7
```

The <var>.index()</var> method returns the index <var>7</var>, which is where the substring <var>"tshah"</var> starts.

**Note:** When using the <var>.index()</var> method to search for substrings, you need to be careful. In the previous example, you want to locate the instance of <var>"tshah"</var>. If you search for just <var>"ts"</var>, Python will return <var>0</var> instead of <var>7</var> because <var>"ts"</var> is also a substring of <var>"tsnow"</var>.

## Key takeaways

As a security analyst, you will work with strings in a variety of ways. First, you might need to use bracket notation to work with string indices. Two functions you will likely use are <var>str()</var>, which converts an input into a string, and <var>len()</var>, which finds the length of a string. You can also use string methods, functions that only work on strings. These include <var>.upper()</var>, which converts all letters in a string into uppercase letters, <var>.lower()</var>, which converts all letters in a string into lowercase letters, and <var>.index()</var>, which returns the index of the first occurrence of its input within a string.

# List operations in Python

Another data type we discussed previously is the list.  
Lists are useful because they allow you to store multiple pieces of data in a single variable.  
In the security profession, you will work with a variety of lists.  
For example, you may have a list of IP addresses that have accessed a network, and another list might hold information on applications that are blocked from running on the system.  
Let's recap how to create a list in Python.  
In this case, the items in our list are the letters A through E.  
We separate them by commas and surround them with square brackets.  
We can also assign our list to a variable to make it easier to use later.  
Here, we've named our variable my\_list.  
When we access specific elements from lists, we use syntax similar to when we access the specific elements from strings.  
We place its index value in brackets after the variable that stores the list.  
So this would access the second item in the list.  
This is because in Python, we start counting the elements in the list at zero and not at one.  
So the index for the first element is zero and the index for the second element is one.  
Let's try extracting some elements from a list.  
We'll extract the second element by putting 1 in brackets after the variable.  
We place this in a print() function to output the results, and after we run it, Python outputs the letter "b".  
Similar to strings, we can also concatenate lists with the plus sign.  
List concatenation is combining two lists into one by placing the elements of the second list directly after the elements of the first list.  
Let's work with this in Python.  
Let's concatenate two lists.  
First, we define the same list as in the previous example and store it in the variable my\_list.  
Now, let's define an additional list with the numbers 1 through 4.  
Finally, let's concatenate the two lists with a plus sign and print out the result.  
And when we run it, we have a final concatenated list.  
Having discussed the similarities, let's now explore the differences between lists and strings.  
We mentioned earlier that strings are immutable, meaning after they are defined, they cannot be changed.  
Lists, on the other hand, do not have this property, and we can freely change, add, and remove list values.  
So, for example, if we have a list of malicious IP addresses, then every time a new malicious IP address is identified, we can easily add it to the list.  
Let's first try changing a specific element in a list in Python.  
We start with the list used in the previous example.  
To change an element in a list, We combine what we learned about bracket notation with what we learned about variable assignment.  
Let's change the second element in my\_list, which is the string "b", to the number 7.  
We place the object we want to change on the left-hand side of the variable assignment.  
In this case, we'll change the second element in my\_list.  
Then we place an equals sign to indicate we are reassigning this element of the list.  
Finally, we place the object to take its place on the right-hand side.  
Here, we'll reassign the second list element to a value of 7.  
Now let's print out the list and run the code to examine the change.  
Perfect!  
The letter "b" is now changed to the number 7.  
Now, let's take a look at methods for inserting and removing elements in lists.  
The first method we'll work with in this video is the insert method.  
The insert method adds an element in a specific position inside a list.  
The method takes two arguments: the first is the position we're adding the element to, and the second is the element we want to add.  
Let's use the insert method.  
We'll start with the list we defined in our my\_list variable.  
Then we type my\_list.insert and pass in two arguments.  
The first argument is the position where we want to insert the new information.  
In this case, we want to insert into index 1.  
The second argument is the information we want to add to the list; in this case, the integer 7.  
Now let's print my\_list.  
Our list still begins with "a", the element with an index of 0, and now, we have the integer 7 in the next position, the position represented with an index of 1.  
Notice that the letter "b", which was originally at index 1, did not get replaced like when we used bracket notation.  
With the insert method, every element beyond index 1 is simply shifted down by one position.  
The index of "b" is now 2.  
Sometimes we might want to remove an element that is no longer needed from a list.  
To do this, we can use the remove method.  
The removed method removes the first occurrence of a specific element in the list.  
Unlike insert, the argument of removed is not an index value.  
Instead, you directly type the element you want to remove.  
The remove method removes the first instance of it in the list.  
Let's use the remove method to delete the letter "d" from our list.  
We'll type the name of our variable my\_list, then add the remove method.  
We want to remove "d"from this list.  
So, we'll place it in quotation marks as our argument.  
Then we'll print my\_list.  
And let's run this.  
Perfect!  
"d" has now been removed from the list.  
Just like with strings, being able to search through lists is a necessary skill for security analysts.  
I'm looking forward to expanding our understanding as we move forward in this course.  
nother data type we discussed previously is the list.  
Lists are useful because they allow you to store multiple pieces of data in a single variable.  
In the security profession, you will work with a variety of lists.  
For example, you may have a list of IP addresses that have accessed a network, and another list might hold information on applications that are blocked from running on the system.  
Let's recap how to create a list in Python.  
In this case, the items in our list are the letters A through E.  
We se.  
​

# Write a simple algorithm

In our everyday lives, we frequently follow rules for solving problems.  
As a simple example, imagine you want a cup of coffee.  
If you've made coffee many times, then you likely follow a process to make it.  
First, you grab your favorite mug.  
Then, you put water into the coffee maker and add your coffee grounds.  
You press the start button and wait a few minutes.  
Finally, you enjoy your fresh cup of coffee.  
Even if you have a different approach to making coffee or don't drink coffee at all, you will likely follow a set of rules for completing similar everyday tasks.  
When you complete these routine tasks, you're following an algorithm.  
An algorithm is a set of rules that solve a problem.  
In more detail, an algorithm is a set of steps that takes an input from a problem, uses this input to perform tasks, and returns a solution as an output.  
Let's explore how algorithms can be used to solve problems in Python.  
Imagine that you, as a security analyst, have a list of IP addresses.  
You want to extract the first three digits of each IP address, which will tell you information about the networks that these IP addresses belong to.  
To do this, we're going to write an algorithm that involves multiple Python concepts that we've covered so far: loops, lists, and strings.  
Here's a list with IP addresses that are stored as strings.  
For privacy reasons, in our example, we're not showing the full IP addresses.  
Our goal is to extract the first three numbers of each address and store them in a new list.  
Before we write any Python code, let's break down an approach to solving this problem with an algorithm.  
What if you had one IP address instead of an entire list?  
Well, then the problem becomes much simpler.  
The first step in solving the problem will be to use string slicing to extract the first three digits from one IP address.  
Now let's consider how to apply these to an entire list.  
As the second step, we'll use a loop to apply that solution to every IP address on the list.  
Previously, you learned about string slicing, so let's write some Python code to solve the problem for one IP address.  
Here we're starting with one IP address that begins with 198.567.  
And we'll write a few lines of code to extract the first three characters.  
We'll use the bracket notation to slice the string.  
Inside the print statement, we have the address variable, which contains the IP address we want to slice.  
Remember that Python starts counting at 0.  
To get the first three characters, we start our slice at index 0 and then continue all the way until index 3.  
Remember, that Python excludes the final index.  
In other words, Python will return the characters at index 0, 1, and 2.  
Now, let's run this.  
We get the first three digits of the address: 198.  
Now that we're able to solve this problem for one IP address, we can put this code into a loop and apply it to all IP addresses in the original list.  
Before we do this, let's introduce one more method that we'll be using in this code: the append method.  
The append method adds input to the end of a list.  
For example, let's say that my list contains 1, 2, and 3.  
With this code, we can use the append method to add 4 to this list.  
First, we are given the IP list.  
Now, we're ready to extract the first three characters from each element in this list.  
Let's create an empty list to store the first three characters of each IP from the list.  
Now we can start the for loop.  
Let's break this down.  
The word "for" tells Python that we're about to start a for loop.  
We then choose address as the variable inside of the for loop, and we specify the list called IP as the iterable.  
As the loop runs, each element from the IP list will be stored temporarily in the address variable.  
Inside the for loop, we have a line of code to add the slice from address to the networks list.  
Breaking this down, we use the code we wrote earlier to get the first three characters of an IP address.  
We'll use our append method to add an item to the end of a list.  
In this case, we're adding to the networks list.  
Finally, let's print the networks list and run the code.  
The variable networks now contains a list of the first three digits of each IP address in the original list: IP.  
That was a lot of information.  
Designing algorithms can be challenging.  
It's a good idea to break them down into smaller problems before jumping into writing your code.  
We'll continue to practice this idea in the upcoming videos.  
Meet you there.

# Lists and the security analyst

Previously, you examined how to use bracket notation to access and change elements in a list and some fundamental methods for working with lists. This reading will review these concepts with new examples, introduce the <var>.index()</var> method as it applies to lists, and highlight how lists are used in a cybersecurity context.

## List data in a security setting

As a security analyst, you'll frequently work with lists in Python. **List data** is a data structure that consists of a collection of data in sequential form. You can use lists to store multiple elements in a single variable. A single list can contain multiple data types.

In a cybersecurity context, lists might be used to store usernames, IP addresses, URLs, device IDs, and data.

Placing data within a list allows you to work with it in a variety of ways. For example, you might iterate through a list of device IDs using a <var>for</var> loop to perform the same actions for all items in the list. You could incorporate a conditional statement to only perform these actions if the device IDs meet certain conditions.

## Working with indices in lists

### Indices

Like strings, you can work with lists through their indices, and indices start at <var>0</var>. In a list, an index is assigned to every element in the list.

This table contains the index for each element in the list <var>\["elarson", "fgarcia", "tshah", "sgilmore"\]</var>:

<div class="rc-CML" dir="auto" id="bkmrk-element-index-%22elars"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="reading_item" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="reading_item"><div class="css-1kgqbsw" data-testid="cml-viewer"><div class="css-1yr0py9"><table><thead><tr><th scope="col">**element**

</th><th scope="col">**index**

</th></tr></thead><tbody><tr><td><var>"elarson"</var>

</td><td><var>0</var>

</td></tr><tr><td><var>"fgarcia"</var>

</td><td><var>1</var>

</td></tr><tr><td><var>"tshah"</var>

</td><td><var>2</var>

</td></tr><tr><td><var>"sgilmore"</var>

</td><td><var>3</var>

</td></tr></tbody></table>

</div></div></div></div></div></div>## Bracket notation

Similar to strings, you can use bracket notation to extract elements or slices in a list. To extract an element from a list, after the list or the variable that contains a list, add square brackets that contain the index of the element. The following example extracts the element with an index of <var>2</var> from the variable <var>username\_list</var> and prints it. You can run this code to examine what it outputs:

```python
username_list = ["elarson", "fgarcia", "tshah", "sgilmore"]
print(username_list[2])
```

```
tshah
```


### **Extracting a slice from a list**

Just like with strings, it's also possible to use bracket notation to take a slice from a list. With lists, this means extracting more than one element from the list.

When you extract a slice from a list, the result is another list. This extracted list is called a sublist because it is part of the original, larger list.

To extract a sublist using bracket notation, you need to include two indices. You can run the following code that takes a slice from a list and explore the sublist it returns:

```python
username_list = ["elarson", "fgarcia", "tshah", "sgilmore"]
print(username_list[0:2])
```

```
['elarson', 'fgarcia']
```

The code returns a sublist of <var>\["elarson", "fgarcia"\]</var>. This is because the element at index <var>0</var>, <var>"elarson"</var>, is included in the slice, but the element at index <var>2</var>, <var>"tshah"</var>, is excluded. The slice ends one element before this index.

## Changing the elements in a list

Unlike strings, you can also use bracket notation to change elements in a list. This is because a string is **immutable** and cannot be changed after it is created and assigned a value, but lists are not immutable.

To change a list element, use similar syntax as you would use when reassigning a variable, but place the specific element to change in bracket notation after the variable name. For example, the following code changes element at index <var>1</var> of the <var>username\_list</var> variable to <var>"bmoreno"</var>.

```python
username_list = ["elarson", "fgarcia", "tshah", "sgilmore"]
print("Before changing an element:", username_list)
username_list[1] = "bmoreno"
print("After changing an element:", username_list)
```

```
Before changing an element: ['elarson', 'fgarcia', 'tshah', 'sgilmore']
After changing an element: ['elarson', 'bmoreno', 'tshah', 'sgilmore']
```

<div class="cml-code-evaluator" id="bkmrk--1"></div>This code has updated the element at index <var>1</var> from <var>"fgarcia"</var> to <var>"bmoreno"</var>.

## List methods

List methods are functions that are specific to the list data type. These include the <var>.insert()</var> , <var>.remove()</var>, <var>.append()</var> and <var>.index()</var>.

### **.insert()** 

The <var>.insert()</var> method adds an element in a specific position inside a list. It has two parameters. The first is the index where you will insert the new element, and the second is the element you want to insert.

You can run the following code to explore how this method can be used to insert a new username into a username list:

```python
username_list = ["elarson", "bmoreno", "tshah", "sgilmore"]
print("Before inserting an element:", username_list)
username_list.insert(2,"wjaffrey")
print("After inserting an element:", username_list)
```

```
Before inserting an element: ['elarson', 'bmoreno', 'tshah', 'sgilmore']
After inserting an element: ['elarson', 'bmoreno', 'wjaffrey', 'tshah', 'sgilmore']
```

Because the first parameter is <var>2</var> and the second parameter is <var>"wjaffrey"</var>, <var>"wjaffrey"</var> is inserted at index <var>2</var>, which is the third position. The other list elements are shifted one position in the list. For example, <var>"tshah"</var> was originally located at index <var>2</var> and now is located at index <var>3</var>.

### **.remove()**

The <var>.remove()</var> method removes the first occurrence of a specific element in a list. It has only one parameter, the element you want to remove.

The following code removes <var>"elarson"</var> from the <var>username\_list</var>:

```python
username_list = ["elarson", "bmoreno", "wjaffrey", "tshah", "sgilmore"]
print("Before removing an element:", username_list)
username_list.remove("elarson")
print("After removing an element:", username_list)
```

```
Before removing an element: ['elarson', 'bmoreno', 'wjaffrey', 'tshah', 'sgilmore']
After removing an element: ['bmoreno', 'wjaffrey', 'tshah', 'sgilmore']
```

This code removes <var>"elarson"</var> from the list. The elements that follow <var>"elarson"</var> are all shifted one position closer to the beginning of the list.

**Note:** If there are two of the same element in a list, the <var>.remove()</var> method only removes the first instance of that element and not all occurrences.

### **.append()** 

The <var>.append()</var> method adds input to the end of a list. Its one parameter is the element you want to add to the end of the list.

For example, you could use <var>.append()</var> to add <var>"btang"</var> to the end of the <var>username\_list</var>:

```python
username_list = ["bmoreno", "wjaffrey", "tshah", "sgilmore"]
print("Before appending an element:", username_list)
username_list.append("btang")
print("After appending an element:", username_list)
```

```
Before appending an element: ['bmoreno', 'wjaffrey', 'tshah', 'sgilmore']
After appending an element: ['bmoreno', 'wjaffrey', 'tshah', 'sgilmore', 'btang']
```

This code places <var>"btang"</var> at the end of the <var>username\_list</var>, and all other elements remain in their original positions.

The <var>.append()</var> method is often used with <var>for</var> loops to populate an empty list with elements. You can explore how this works with the following code:

```python
numbers_list = []
print("Before appending a sequence of numbers:", numbers_list)
for i in range(10):
    numbers_list.append(i)
print("After appending a sequence of numbers:", numbers_list)
```

```
Before appending a sequence of numbers: []
After appending a sequence of numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

Before the <var>for</var> loop, the <var>numbers\_list</var> variable does not contain any elements. When it is printed, the empty list is displayed. Then, the <var>for</var> loop iterates through a sequence of numbers and uses the <var>.append()</var> method to add each of these numbers to <var>numbers\_list</var>. After the loop, when the <var>numbers\_list</var> variable is printed, it displays these numbers.

## .index()

Similar to the <var>.index()</var> method used for strings, the <var>.index()</var> method used for lists finds the first occurrence of an element in a list and returns its index. It takes the element you're searching for as an input.

**Note:** Although it has the same name and use as the <var>.index()</var> method used for strings, the <var>.index()</var> method used for lists is not the same method. Methods are defined when defining a data type, and because strings and lists are defined differently, the methods are also different.

Using the <var>username\_list</var> variable, you can use the <var>.index()</var> method to find the index of the username <var>"tshah"</var>:

```python
username_list = ["bmoreno", "wjaffrey", "tshah", "sgilmore", "btang"]
username_index = username_list.index("tshah")
print(username_index)
```

```
2
```

Because the index of <var>"tshah"</var> is <var>2</var>, it outputs this number.

Similar to the <var>.index()</var> method used for strings, it only returns the index of the first occurrence of a list item. So if the username <var>"tshah"</var> were repeated twice, it would return the index of the first instance, and not the second.

## Key takeaways

Python offers a lot of ways to work with lists. Bracket notation allows you to extract elements and slices from lists and also to alter them. List methods allow you to alter lists in a variety of ways. The <var>.insert()</var> and <var>.append()</var> methods add elements to lists while the <var>.remove()</var> method allows you to remove them. The <var>.index()</var> method allows you to find the index of an element in a list.

# Regular expressions in Python

We've already learned a lot about working with strings.  
This includes working with their positional indices and slicing them.  
In the previous video, we applied these to extract the first three digits from a list of IP addresses.  
In this video, we're going to focus on a more advanced way to search through strings.  
We'll learn about searching for patterns in strings through regular expressions.  
A regular expression, shortened to regex, is a sequence of characters that forms a pattern.  
This pattern can be used when searching within log files.  
We can use them to search for any kind of pattern.  
For example, we can find all strings that start with a certain prefix, or we can find all strings that are a certain length.  
We can apply this to a security context in a variety of ways.  
For example, let's say we needed to find all IP addresses with a network ID of 184.  
Regular expressions would allow us to efficiently search for this pattern.  
We'll examine another example throughout this video.  
Let's say that we want to extract all the email addresses containing a log.  
If we try to do this through the index method, we would need the exact email addresses we were searching for.  
As security analysts, we rarely have that kind of information.  
But if we use a regular expression that tells Python how an email address is structured, it would return all the strings that have the same elements as an email address.  
Even if we were given a log file with thousands of lines and entries, we could extract every email in the file by searching for the structure of an email address through a regular expression.  
We wouldn't need to know the specific emails to extract them.  
Let's explore the regular expression symbols that we need to do this.  
To begin, let's learn about the plus sign.  
The plus sign is a regular expression symbol that represents one or more occurrences of a specific character.  
Let's explain that through an example pattern.  
The regular expression pattern a+ matches a string of any length in which "a" is repeated.  
For example, just a single "a", three "a's" in a row, or five "a's" in a row.  
It could even be 1000 "a's" in a row.  
We can start working with a quick example to see which strings this pattern would extract.  
Let's start with this string of device IDs.  
These are all the instances of the letter "a" written once or multiple times in a row.  
The first instance has one "a", the second has two "a's", the third one has one "a", and the fourth has three "a's".  
So, if we told Python to find matches to the a+ sign regular expression, it would return this list of "a's".  
The other building block we need is the \\w symbol.  
This matches with any alphanumeric character, but it doesn't match symbols.  
"1", "k", and "i" are just three examples of what "\\w" matches.  
Regular expressions can easily be combined to allow for even more patterns in a search.  
Before we apply this to our email context, let's explore the patterns we can search for if we combine the "\\w" with the plus sign.  
"\\w" matches any alphanumeric character, and the plus sign matches any number of occurrences of the character before it.  
This means that the combination of "\\w+" matches an alphanumeric string of any length.  
"\\w" provides flexibility in the alphanumeric characters that this regular expression matches, and the plus sign provides flexibility in the length of the string that it matches.  
The strings "192", "abc123", and "security" are just three possible strings that match to "\\w+".  
Now let's apply these to extracting email addresses from a log.  
Email addresses consist of text separated by certain symbols, like the @ symbol and the period.  
Let's learn how we can represent this as a regular expression.  
To start, let's think about the format of a typical email address; for example, user1@email1.com.  
The first segment of an email address contains alphanumeric characters, and the number of alphanumeric characters may vary in length.  
We can use our regular expression "\\w+" for this portion to match to an alphanumeric string of any length.  
The next segment in an email address is the @ symbol.  
This segment is always present.  
We'll enter this directly in our regular expression.  
Including this is essential for ensuring that Python distinguishes email addresses from other strings.  
After the @ symbol is the domain name.  
Just like the first segment, this one varies depending on the email address, but it always contains alphanumeric characters, so we can use "\\w+" again to allow for this variation.  
Next, just like the @ symbol, a period is always part of an email address.  
But unlike the @ symbol, in regular expressions, the period has a special meaning.  
For this reason, we need to use backslash period here.  
When we add a backslash in front of it, we let Python know that we are not intending to use it as an operator, and that our pattern should include a period in this location.  
For the last segment, we can also use "\\w+".  
This final part of an email address is often "com" but might be other strings like "net." When we put the pieces together, we get the regular expression we'll use to find email addresses in our row.  
This pattern will match all email addresses.  
It will exclude everything else in our string.  
This is because we've included the @ symbol and the period where they appear in the structure of an email address.  
Let's bring this into Python.  
We'll use regular expressions to extract email addresses from a string.  
Regular expressions can be used when the re module is imported into Python, so we begin with that step.  
Later, we'll learn how to import and open files like logs.  
But for now, we've restored our log as a string variable named email\_log.  
Because this is a multi-line string, we're using three sets of quotation marks instead of just one.  
Next, we'll apply the findall() function from the re module to a regular expression.  
re.findall() returns a list of matches to a regular expression.  
Let's use this with the regular expression we created earlier for email addresses.  
The first argument is the pattern that we want to match.  
Notice that we place it in quotation marks.  
The second argument indicates where to search for the pattern.  
In this case, we're searching through the string contained within the email log variable.  
When we run this, we get a list of all the emails in the string.  
Imagine applying this to a log with thousands of entries.  
Pretty useful, right?  
This was just an introduction to the power of regular expressions.  
There are many more symbols you can use.  
I encourage you to explore regular expressions on your own and learn more.

# More about regular expressions

You were previously introduced to regular expressions and a couple of symbols that you can use to construct regular expression patterns. In this reading, you'll explore additional regular expression symbols that can be used in a cybersecurity context. You'll also learn more about the <var>re</var> module and its <var>re.findall()</var> function.

## Basics of regular expressions

A **regular expression (regex)** is a sequence of characters that forms a pattern. You can use these in Python to search for a variety of patterns. This could include IP addresses, emails, or device IDs.

To access regular expressions and related functions in Python, you need to import the <var>re</var> module first. You should use the following line of code to import the <var>re</var> module:

<var>import re</var>

Regular expressions are stored in Python as strings. Then, these strings are used in <var>re</var> module functions to search through other strings. There are many functions in the <var>re</var> module, but you will explore how regular expressions work through <var>re.findall()</var>. The <var>re.findall()</var> function returns a list of matches to a regular expression. It requires two parameters. The first is the string containing the regular expression pattern, and the second is the string you want to search through.

The patterns that comprise a regular expression consist of alphanumeric characters and special symbols. If a regular expression pattern consists only of alphanumeric characters, Python will review the specified string for matches to this pattern and return them. In the following example, the first parameter is a regular expression pattern consisting only of the alphanumeric characters <var>"ts"</var>. The second parameter, <var>"tsnow, tshah, bmoreno"</var>, is the string it will search through. You can run the following code to explore what it returns:

```python
import re
re.findall("ts", "tsnow, tshah, bmoreno")
```

```
['ts', 'ts']
```

The output is a list of only two elements, the two matches to <var>"ts"</var>: <var>\['ts', 'ts'\]</var>.

If you want to do more than search for specific strings, you must incorporate special symbols into your regular expressions.

## Regular expression symbols

### Symbols for character types

You can use a variety of symbols to form a pattern for your regular expression. Some of these symbols identify a particular type of character. For example, <var>\\w</var> matches with any alphanumeric character.

**Note:** The <var>\\w</var> symbol also matches with the underscore (<var> \_ </var>).

You can run this code to explore what <var>re.findall()</var> returns when applying the regular expression of <var>"\\w"</var> to the device ID of <var>"h32rb17"</var>.

```python
import re
re.findall("\w", "h32rb17")
```

```
['h', '3', '2', 'r', 'b', '1', '7']
```

Because every character within this device ID is an alphanumeric character, Python returns a list with seven elements. Each element represents one of the characters in the device ID.

You can use these additional symbols to match to specific kinds of characters:

- <var>.</var> matches to all characters, including symbols
- <var>\\d</var> matches to all single digits \[0-9\]
- <var>\\s</var> matches to all single spaces
- <var>\\.</var> matches to the period character

The following code searches through the same device ID as the previous example but changes the regular expression pattern to <var>"\\d"</var>. When you run it, it will return a different list:

```python
import re
re.findall("\d", "h32rb17")
```

```
['3', '2', '1', '7']
```

This time, the list contains only four elements. Each element is one of the numeric digits in the string.

### Symbols to quantify occurrences

Other symbols quantify the number of occurrences of a specific character in the pattern. In a regular expression pattern, you can add them after a character or a symbol identifying a character type to specify the number of repetitions that match to the pattern.

For example, the <var>+</var> symbol represents one or more occurrences of a specific character. In the following example, the pattern places it after the <var>"\\d"</var> symbol to find matches to one or more occurrences of a single digit:

```python
import re
re.findall("\d+", "h32rb17")
```

```
['32', '17']
```

With the regular expression <var>"\\d+"</var>, the list contains the two matches of <var>"32"</var> and <var>"17"</var>.

Another symbol used to quantify the number of occurrences is the <var>\*</var> symbol. The <var>\*</var> symbol represents zero, one, or more occurrences of a specific character. The following code substitutes the <var>\*</var> symbol for the <var>+</var> used in the previous example. You can run it to examine the difference:

```python
import re
re.findall("\d*", "h32rb17")
```

```
['', '32', '', '', '17', '']
```

Because it also matches to zero occurrences, the list now contains empty strings for the characters that were not single digits.

If you want to indicate a specific number of repetitions to allow, you can place this number in curly brackets (<var>{ }</var>) after the character or symbol. In the following example, the regular expression pattern <var>"\\d{2}"</var> instructs Python to return all matches of exactly two single digits in a row from a string of multiple device IDs:

```python
import re
re.findall("\d{2}", "h32rb17 k825t0m c2994eh")
```

```
['32', '17', '82', '29', '94']
```

Because it is matching to two repetitions, when Python encounters a single digit, it checks whether there is another one following it. If there is, Python adds the two digits to the list and goes on to the next digit. If there isn't, it proceeds to the next digit without adding the first digit to the list.

**Note:** Python scans strings left-to-right when matching against a regular expression. When Python finds a part of the string that matches the first expected character defined in the regular expression, it continues to compare the subsequent characters to the expected pattern. When the pattern is complete, it starts this process again. So in cases in which three digits appear in a row, it handles the third digit as a new starting digit.

You can also specify a range within the curly brackets by separating two numbers with a comma. The first number is the minimum number of repetitions and the second number is the maximum number of repetitions. The following example returns all matches that have between one and three repetitions of a single digit:

```python
import re
re.findall("\d{1,3}", "h32rb17 k825t0m c2994eh")
```

```
['32', '17', '825', '0', '299', '4']
```

The returned list contains elements of one digit like <var>"0"</var>, two digits like <var>"32"</var> and three digits like <var>"825"</var>.

### Constructing a pattern

Constructing a regular expression requires you to break down the pattern you're searching for into smaller chunks and represent those chunks using the symbols you've learned. Consider an example of a string that contains multiple pieces of information about employees at an organization. For each employee, the following string contains their employee ID, their username followed by a colon (<var>:</var>), their attempted logins for the day, and their department:

<var>employee\_logins\_string = "1001 bmoreno: 12 Marketing 1002 tshah: 7 Human Resources 1003 sgilmore: 5 Finance"</var>

Your task is to extract the username and the login attempts, without the employee's ID number or department.

To complete this task with regular expressions, you need to break down what you're searching for into smaller components. In this case, those components are the varying number of characters in a username, a colon, a space, and a varying number of single digits. The corresponding regular expression symbols are <var>\\w+</var>, <var>:</var>, <var>\\s</var>, and <var>\\d+</var> respectively. Using these symbols as your regular expression, you can run the following code to extract the strings:

```python
import re
pattern = "\w+:\s\d+"
employee_logins_string = "1001 bmoreno: 12 Marketing 1002 tshah: 7 Human Resources 1003 sgilmore: 5 Finance"
print(re.findall(pattern, employee_logins_string))
```

```
['bmoreno: 12', 'tshah: 7', 'sgilmore: 5']
```

**Note:** Working with regular expressions can carry the risk of returning unneeded information or excluding strings that you want to return. Therefore, it's useful to test your regular expressions.

## Key takeaways

Regular expressions allow you to search through strings to find matches to specific patterns. You can use regular expressions by importing the <var>re</var> module. This module contains multiple functions, including <var>re.findall()</var>, which returns all matches to a pattern in the form of a list. To form a pattern, you use characters and symbols. Symbols allow you to specify types of characters and to quantify how many repetitions of a character or type of character can occur in the pattern.

# Wrap-up

Congratulations!  
We accomplished a lot together.  
Let's take time to quickly go through all the new concepts we covered.  
We started this course by focusing on working with strings and lists.  
We learned methods that work specifically with these data types.  
We also learned to work with indices and extract information we need.  
Next, we focused on writing algorithms.  
We wrote a simple algorithm that sliced the network ID from a list of IP addresses.  
Finally, we covered using regular expressions.  
Regular expressions allow you to search for patterns, and this provides expanded ways to locate what you need in logs and other files.  
These are complex concepts, and you're always welcome to visit the videos again whenever you like.  
With these concepts, you took a big step towards being able to work with data and write the algorithms that security professionals need.  
Throughout the rest of this course, you're going to get more practice with Python and what it can offer to security analysts.  
​

# Reference guide: Python concepts from module 3; Terms and definitions from Course 7, Module 3

<span style="font-size: 16pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Built-in functions</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The following built-in functions are commonly used in Python.</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">str()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Converts the input object to a string</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">str(10)</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Converts the integer </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">10</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> to the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"10"</span>


### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">len()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns the number of elements in an object</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print(len("security"))</span>

## <span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns and displays </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">8</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">, the number of characters in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"security"</span>

## <span style="font-size: 16pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">String methods</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The following methods can be applied to strings in Python. </span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.upper()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a copy of the string in all uppercase letters</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print("Security".upper())</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns and displays a copy of the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"Security"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> as </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"SECURITY"</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.lower()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a copy of the string in all lowercase letters</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print("Security".lower())</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns and displays a copy of the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"Security"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> as </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"security"</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.index()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Finds the first occurrence of the input in a string and returns its location</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print("Security".index("c"))</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Finds the first occurrence of the character </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"c"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"Security"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> and returns and displays its index of </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">2</span>


## <span style="font-size: 16pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">List methods</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The following methods can be applied to lists in Python. </span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.insert()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Adds an element in a specific position inside the list</span>

<span style="font-size: 13pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list = \["elarson", "fgarcia", "tshah"\]</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list.insert(2,"wjaffrey")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Adds the element </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"wjaffrey"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> at index </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">2</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> to the </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; the list becomes </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["elarson", "fgarcia", "wjaffrey", "tshah"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.remove()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Removes the first occurrence of a specific element inside a list</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list = \["elarson", "bmoreno", "wjaffrey", "tshah"\]</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list.remove("elarson")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Removes the element </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"elarson"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> from the </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; the list becomes </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["fgarcia", "wjaffrey", "tshah"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.append()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Adds input to the end of a list</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list = \["bmoreno", "wjaffrey", "tshah"\]</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list.append("btang")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Adds the element </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"btang"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> to the end of the </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; the list becomes </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["fgarcia", "wjaffrey", "tshah", "btang"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.index()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Finds the first occurrence of an element in a list and returns its index</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list = \["bmoreno", "wjaffrey", "tshah", "btang"\]</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print(username\_list.index("tshah"))</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Finds the first occurrence of the element </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"tshah"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> and returns and displays its index of </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">2</span>


## <span style="font-size: 16pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Additional syntax for working with strings and lists</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The following syntax is useful when working with strings and lists. </span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">+ </span><span style="font-size: 14pt; font-family: 'Google Sans', sans-serif; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">(concatenation)</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Combines two strings or lists together</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">device\_id = "IT"+"nwp12"</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Combines the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"IT"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> with the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"nwp12"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> and assigns the combined string of </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"ITnwp12"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> to the variable </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">device\_id</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">users = \["elarson", "bmoreno"\] + \["tshah", "btang"\]</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Combines the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["elarson", "bmoreno"\]</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> with the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["tshah", "btang"\]</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> and assigns the combined list of </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["elarson", "bmoreno", "tshah", "btang"\]</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> to the variable </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">users</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\[\] </span><span style="font-size: 14pt; font-family: 'Google Sans', sans-serif; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">(bracket notation)</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Uses indices to extract parts of a string or list </span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print("h32rb17"\[0\])</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Extracts the character at index </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">0</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">, which is (</span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"h"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">), from the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"h32rb17"</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print("h32rb17"\[0:3\])</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Extracts the slice </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\[0:3\]</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">, which is (</span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"h32"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">), from the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"h32rb17"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; the first index in the slice (</span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">0</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">) is included in the slice but the second index in the slice (</span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">3</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">) is excluded</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list = \["elarson", "fgarcia", "tshah"\]</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">print(username\_list\[2\])</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Extracts the element at index </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">2</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">, which is (</span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"tshah"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">), from the </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">username\_list</span>


## <span style="font-size: 16pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Regular expressions</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The following </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> module function and regular expression symbols are useful when searching for patterns in strings. </span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall()</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to a regular expression</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("a53", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["a53"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\\w</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Matches with any alphanumeric character; also matches with the underscore (</span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\_</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">)</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("\\w", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"\\w"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to any alphanumeric character and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["a", "5", "3", "3", "2", "c", "E"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Matches to all characters, including symbols</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall(".", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"."</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to all characters and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["a", "5", "3", "-", "3", "2", "c", " ", ".", "E"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\\d</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Matches to all single digits</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("\\d", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"\\d"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to all single digits and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["5", "3", "3", "2"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\\s</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Matches to all single spaces</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("\\d", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"\\s"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to all single spaces and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\[" "\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\\.</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Matches to the period character</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("\\.", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"\\."</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to all instances of the period character and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["."\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">+</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Represents one or more occurrences of a specific character</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("\\w+", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"\\w+"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to one or more occurrences of any alphanumeric character and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["a53", "32c", "E"\]</span>


### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\*</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Represents, zero, one or more occurrences of a specific character</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("\\w\*", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"\\w\*"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to one or more occurrences of any alphanumeric character and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["a53", " ", "32c", " ", " ", "E"\]</span>

### <span style="font-size: 14pt; font-family: 'Courier New', monospace; font-weight: bold; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">{ }</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Represents a specified number of occurrences of a specific character; the number is specified within the curly brackets</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">import re</span>

<span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">re.findall("\\w{3}", "a53-32c .E")</span>

<span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Returns a list of matches to the regular expression pattern </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"\\w{3}"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> in the string </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">"a53-32c .E"</span><span style="font-size: 12pt; font-family: 'Google Sans', sans-serif; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">; matches to exactly three occurrences of any alphanumeric character and returns the list </span><span style="font-size: 12pt; font-family: 'Courier New', monospace; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">\["a53","32c"\]</span>

---

# Glossary terms from module 3

**Algorithm:** A set of rules that solve a problem

**Bracket notation:** The indices placed in square brackets

**Debugging:** The practice of identifying and fixing errors in code

**Immutable:** An object that cannot be changed after it is created and assigned a value

**Index:** A number assigned to every element in a sequence that indicates its position

**List concatenation:** The concept of combining two lists into one by placing the elements of the second list directly after the elements of the first list

**List data:** Data structure that consists of a collection of data in sequential form

**Method:** A function that belongs to a specific data type

**Regular expression (regex):** A sequence of characters that forms a pattern

**String concatenation:** The process of joining two strings together

**String data:** Data consisting of an ordered sequence of characters

**Substring:** A continuous sequence of characters within a string