COURSE 7 – AUTOMATE CYBERSECURITY TASKS WITH PYTHON

Module 3: Work with Strings and Lists 

GOOGLE ADVANCED DATA ANALYTICS PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

INTRODUCTION – Work with Strings and Lists

In this extended overview, participants will delve deeper into the versatile realm of Python programming, with a specific emphasis on enhancing proficiency in working with strings and lists. The module unfolds a spectrum of methods tailored for these fundamental data types, providing participants with an enriched toolkit for data manipulation and algorithmic problem-solving. Through practical applications, participants will have the opportunity to synthesize their newfound knowledge, culminating in the creation of a succinct yet impactful algorithm.

The journey extends further into the realm of regular expressions, offering participants insights into a powerful tool for pattern recognition in strings. By exploring the intricacies of regular expressions, participants will gain a nuanced understanding of how to search for and identify specific patterns within textual data. This comprehensive module not only equips participants with advanced Python programming skills but also nurtures their ability to harness these skills for practical problem-solving scenarios.

Learning Objectives

  • Use Python to work with strings and lists.
  • Write a simple algorithm.
  • Use regular expressions to extract information from text.

TEST YOUR KNOWLEDGE: WORK WITH STRINGS

1. Which of the following statements correctly describes strings? Select all that apply.

  • Strings are immutable. (CORRECT)
  • Strings must be placed in brackets ([ ]).
  • Strings cannot contain numeric characters.
  • Strings must be placed in quotation marks (” “). (CORRECT)

Strings must be placed in quotation marks. Strings are also immutable. This means they cannot be changed after they are created and assigned a value.

2. What does the following code return?

device_id = "uu0ktt0vwugjyf2"
print(device_id[2:5])
  • “u0kt”
  • “0kt” (CORRECT)
  • “u0k”
  • “0ktt”

This code returns “0kt”. It uses bracket notation to take a slice of the value contained in the device_id variable.  Indices start at 0 in Python. It extracts the characters at indices 2, 3, and 4. The character at index 5 is excluded from the slice.

3. What does the following code display?

device_id = "Tj1C58Dakx"
print(device_id.lower())
  • “tj1C58Dakx”
  • “Tj1C58Dakx”
  • “tj1c58dakx” (CORRECT)
  • “TJ1C58DAKX”

This code displays “tj1c58dakx”. The .lower() method converts all uppercase characters into lowercase characters.

4. You want to find the index where the substring “192.168.243.140” starts within the string contained in the variable ip_addresses. Complete the Python code to find and display the starting index. (If you want to undo your changes to the code, you can click the Reset button.)

"192.168.243.140" Cybersecurity - Course 7
Coursera Image
"192.168.243.140" Cybersecurity - Course 7 - Answer
Coursera Image

What index does the substring “192.168.243.140” start at?

  • 31
  • 32 (CORRECT)
  • 34
  • 33

The substring “192.168.243.140” starts at index 32. You can determine this using the code ip_addresses.index(“192.168.243.140”). Note that Python indices start at 0.

5. What does the code print(“HELLO”[2:4]) output?

  • “LL” (CORRECT)
  • “E”
  • “EL”
  • “LLO”

The code print(“HELLO”[2:4]) outputs “LL”. The first index in the slice is included in the output, but the second index in the slice is not included. This means the slice starts at the character at index 2 and ends one character before index 4.

TEST YOUR KNOWLEDGE: WORK WITH LISTS AND DEVELOP ALGORITHMS

1. Review the following code:

my_list = ["a", "b", "c", "d"]
my_list[2] = 4
print(my_list)

What will it display?

  • [“a”, “b”, “4”, “d”]
  • An error message
  •  [“a”, “b”, 4, “d”] (CORRECT)
  •  [“a”, 4, “c”, “d”]

The code will display [“a”, “b”, 4, “d”]. It reassigns the my_list element at index 2. This is the third element in the list, so “c” is replaced by the integer 4.

2. You are working with the list [“cwvQSQ”,”QvPvX5″,”ISyT3a”,”S7vgN0″]. Its elements represent machine IDs, and the list is stored in a variable named machine_ids. Which line of code will add the ID of “yihhLL” at index 3?

  • machine_ids.insert(“yihhLL”,3)
  • machine_ids.append(“yihhLL”,3)
  • machine_ids.insert(3,”yihhLL”) (CORRECT)
  • machine_ids.append(“yihhLL”)

The code machine_ids.insert(3,”yihhLL”) will add the ID of “yihhLL” at index 3. The .insert() method adds an element in a specific position inside a list. It takes in two parameters. The first indicates the index where you want to add a new element, and the second indicates the element you want to add.

3. Which line of code will remove the username “tshah” from the following list?

access_list = [“elarson”, “bmoreno”, “tshah”, “sgilmore”]

  • access_list.remove(3)
  • access_list.remove(“tshah”) (CORRECT)
  • access_list.remove(2)
  • access_list[“tshah”].remove()

The code access_list.remove(“tshah”) will remove the username “tshah” from the list. The .remove() method removes the first occurrence of a specific element in a list. It takes the element to be removed as its argument, so access_list.remove(“tshah”) removes the username “tshah”.

4. As a security analyst, you are responsible for developing an algorithm that automates removing usernames that match specific criteria from an access list. What Python components would help you implement this? Select three answers.

  • A for loop that iterates through the usernames in the access list (CORRECT)
  • The .append() method
  • The .remove() method (CORRECT)
  • An if statement that compares a username to the criteria for removal (CORRECT)

The algorithm should iterate through the usernames in an access list. On each iteration, it should check whether the current username matches the specific criteria and remove it when it does. The if statement checks if the current username matches the specific criteria.

5. In the list [“elarson”, “bmoreno”, “tshah”, “eraab”], which element has an index of 3?

  • “bmoreno”
  • “tshah”
  • “eraab” (CORRECT)
  • “elarson”

In the list [“elarson”, “bmoreno”, “tshah”, “eraab”], the element “eraab” has an index of 3. In Python, indices start at 0, so the element that has an index of 3 is the fourth element.

TEST YOUR KNOWLEDGE: REGULAR EXPRESSIONS

1. Which regular expression symbol represents one or more occurrences of a specific character?

  • + (CORRECT)
  • *
  • \d
  • \w

The symbol + represents one or more occurrences of a specific character.

2. As a security analyst, you are responsible for finding employee IDs that end with the character and number sequence “a6v”. Given that employee IDs consist of both numbers and alphabetic characters and are at least four characters long, which regular expression pattern would you use?

  • “\w+a6v” (CORRECT)
  • “\wa6v”
  • “\w*a6v”
  • “a6v”

The regular expression “\w+a6v” matches strings that consist of both numbers and alphabetic characters, are at least four characters long, and end with the sequence “a6v”. There must be at least one other character before “a6v”, so “\w+” is needed to match to one or more alphanumeric characters. Then, the required ending sequence is “a6v”.

3. You have imported the re module into Python with the code import re. You want to use the findall() function to search through a string. Which function call enables you to search through the string contained in the variable text in order to return all matches to a regular expression stored in the variable pattern?

  • re.findall(text, pattern)
  • findall(text, pattern)
  • findall(pattern, text)
  • re.findall(pattern, text) (CORRECT)

The function call re.findall(pattern, text) enables you to do this. The re.findall() function returns a list of matches to a regular expression. You must specify that this function comes from the re module. The first argument is the regular expression pattern that you want to match. In this case, it is found in the variable pattern. The second argument indicates where to search for this pattern. In this case, this is the string assigned to the variable text.

4. Which of the following strings would Python return as matches to the regular expression pattern “\w+”? Select all that apply.

  • “FirstName” (CORRECT)
  • “”
  • “3” (CORRECT)
  • “#name”

The strings “3” and “FirstName” match the regular expression pattern “\w+”. The \w symbol matches with any alphanumeric character. When combined with the + symbol, it represents one or more occurrences of any alphanumeric character. Because “FirstName” is a string that contains multiple alphanumeric characters, it matches the regular expression.

The strings “3” and “FirstName” match the regular expression pattern “\w+”. The \w symbol matches with any alphanumeric character. When combined with the + symbol, it represents one or more occurrences of any alphanumeric character. Because “3” is a string that contains one alphanumeric character, it matches the regular expression.

5. Which string matches with the regular expression “b\wa+b”?

  • “bkaaab” (CORRECT)
  • “yaaab”
  • “cba”
  • “baaa”

The string “bkaab” matches with the regular expression “b\wa+b”. The first character must be “b”. After this, the symbol \w is used to match any alphanumeric character, including “k”. Next, the + symbol specifies that there should be one or more occurrences of the character it follows, which in this case is “a”. Finally, the string must end with “b”.

MODULE 3 CHALLENGE

1. What is the output of the following code?

print(len("125"))
  • 5
  • 3 (CORRECT)
  • 10
  • 8

2. Which line of code returns a copy of the string “bmoreno” as “BMORENO”?

  • print(upper.”bmoreno”())
  • print(“bmoreno”.upper()) (CORRECT)
  • print(“bmoreno”(upper))
  • print(upper(“bmoreno”))

3. In the string “network”, which character has an index of 1?

  • “e” (CORRECT)
  • “n”
  • “k”
  • “t”

4. You need to take a slice from a network ID. Specifically, you must extract the characters with indices of 6 through 10. Complete the Python code to take this slice and display it. (If you want to undo your changes to the code, you can click the Reset button.)

network ID - course 7 - work with string and list
Coursera img
network ID - course 7 - work with string and list - answer
Coursera img

What string does the code output?

  • “85n52” (CORRECT)
  • “5n528”
  • “585n5”
  • “m585n”

5. What is the output of the following code?

username_list  = ["elarson", "bmoreno", "tshah"] 
device_id_list = ["us2c0R5", "2R78TBR", "bt3MIEz"]
print(username_list + device_id_list)
  •  [“elarson”, “us2c0R5”, “bmoreno”, “2R78TBR”, “tshah”, “bt3MIEz”]
  •  [“us2c0R5”, “2R78TBR”, “bt3MIEz”, “elarson”, “bmoreno”, “tshah”]
  • An error message
  •  [“elarson”, “bmoreno”, “tshah”, “us2c0R5”, “2R78TBR”, “bt3MIEz”] (CORRECT)

6. A variable named my_list contains the list [1,2,3,4]. Which line of code adds the element 5 to the end of the list?

  • my_list.insert(4,5) (CORRECT)
  • my_list.insert(5,5)
  • my_list.insert(5,4)
  • my_list.insert(5)

Strings must be placed in quotation marks. Strings are also immutable. This means they cannot be changed after they are created and assigned a value.

7. Fill in the blank: Determining that you need to use string slicing and a for loop to extract information from items in a list is part of creating a(n) _____.

  • regular expression
  • append
  • index
  • algorithm (CORRECT)

8. Which of the following strings would Python return as matches to the regular expression of “\w+”? Select all that apply.

  • “email@email.com”
  • “network” (CORRECT)
  • “9210” (CORRECT)
  • “email123” (CORRECT)

9. What module do you need to import to use regular expressions in Python?

  • re (CORRECT)
  • time
  • os
  • csv

10. What does the code username_list.append(“bmoreno”) method do?

  • Updates all instances of “bmoreno” in the username_list list to uppercase letters
  • Returns all matches to the pattern “bmoreno” in the username_list list
  • Adds “bmoreno” to the end of the username_list list (CORRECT)
  • Inserts “bmoreno” at the beginning of the username_list list

11. Which line of code converts the integer 7 to a string?

  • str(7) (CORRECT)
  • string(“7”)
  • string(7)
  • str(“7”)

12. Which line of code returns a copy of the string “HG91AB2” as “hg91ab2”?

  • print(“HG91AB2″(lower))
  • print(“HG91AB2”.lower()) (CORRECT)
  • print(lower.”HG91AB2″())
  • print(lower(“HG91AB2”))

13. What is the index of the character “4” in the string “h204D3921”?

  • 3 (CORRECT)
  • 2
  • 5
  • 4

14. You need to take a slice from an employee ID. Specifically, you must extract the characters with indices of 3, 4, 5, and 6. Complete the Python code to take this slice and display it. (If you want to undo your changes to the code, you can click the Reset button.)

Complete the Python code - Question
Coursera img

What string does the code output?

Complete the Python code - Answer
Coursera img
  • “237x”
  • “x430”
  • “7×43” (CORRECT)
  • “37×4”

15. What is the output of the following code?

list1 = [1, 2, 3] 
list2 = ["a", "b", "c"]
print(list1 + list2)
  •  [6, “abc”]
  • An error message
  •  [1, 2, 3, “a”, “b”, “c”] (CORRECT)
  •  [1, “a”, 2, “b”, 3, “c”]

16. What is an algorithm?

  • A function that finds matches to a pattern
  • A set of guidelines to keep code consistent
  • A set of rules to solve a problem (CORRECT)
  • A function that returns information

Strings must be placed in quotation marks. Strings are also immutable. This means they cannot be changed after they are created and assigned a value.

17. What does the \w symbol match to in a regular expression?

  • Any character and symbol
  • Any alphanumeric character (CORRECT)
  • Any number
  • Any letter

18. What does the re.findall() function return?

  • All possible regular expressions that match to a given string
  • A list of all matches to a regular expression in a given string (CORRECT)
  • The first match to a regular expression in a given string
  • All occurrences of the pattern “re” in a given string

19. Which code joins a list of new_users to a list of approved_users and assigns the value to a third variable named users?

  • users(new_users, approved_users)
  • users = new_users + approved_users (CORRECT)
  • users = insert(new_users, approved_users)
  • users(new_users[1], approved_users[2])

20. Which of the following strings would Python return as matches to the regular expression pattern of  “\w”? Select all that apply.

  • “1B”
  • “security”
  • “W” (CORRECT)
  • “2” (CORRECT)

21. You have imported the re module into Python with the code import re. Which code searches the device_ids string variable for a pattern of “r15\w+”?

  • findall(“r15\w+”, device_ids)
  • re.findall(device_ids, “r15\w+”)
  • findall(device_ids, “r15\w+”)
  • re.findall(“r15\w+”, device_ids) (CORRECT)

22. Which method adds input to the end of a list?

  • .lower()
  • .insert()
  • .append() (CORRECT)
  • .index()

23. Which line of code returns the number of characters in the string assigned to the username variable?

  • print(len(username)) (CORRECT)
  • print(username.len())
  • print(str(username))
  • print(username.str())

24. What is the result when .upper() is applied to a string?

  • A copy of the string is returned with all uppercase letters. (CORRECT)
  • The character that appears most frequently in the string is extracted from it and returned.
  • The value of the string is reassigned to the value of the string in the line preceding it.
  • The value of the string is reassigned to contain all uppercase letters.

25. What is the output of the following code?

approved_users = ["bmoreno", "elarson", "tshah", "eraab"]
print(approved_users[1])
  • [“bmoreno”, “elarson”, “tshah”, “eraab”, 1]
  •  [1, “bmoreno”, “elarson”, “tshah”, “eraab”]
  • “bmoreno”
  • “elarson” (CORRECT)

26. What does the code device_ids.append(“h32rb17”) do?

  • Inserts “h32rb17” at the beginning of the device_ids list
  • Updates all instances of “h32rb17” in the device_ids list to uppercase letters
  • Returns all matches to the pattern “h32rb17” in the device_ids list
  • Adds “h32rb17” to the end of the device_ids list (CORRECT)

27. What is the index of the character “c” in the string “encryption”?

  • 4
  • 3
  • 1
  • 2 (CORRECT)

28. You need to take a slice from a device ID. Specifically, you must extract the characters with indices of 8, 9, and 10. Complete the Python code to take this slice and display it. (If you want to undo your changes to the code, you can click the Reset button.)

extract the characters with indices of 8, 9, and 10 - Question
Coursera img

What string does the code output?

extract the characters with indices of 8, 9, and 10 - Answer
Coursera img
  • “1w3”
  • “81w”
  • “w36” (CORRECT)
  • “363”

CONCLUSION – Work with Strings and Lists

In conclusion, this module serves as a pivotal step in the participants’ Python programming journey, elevating their proficiency in manipulating strings and lists. The exploration of advanced methods for these data types and the practical application of algorithmic concepts contribute to a well-rounded skill set.

The incorporation of regular expressions enhances participants’ capabilities in pattern recognition within strings, fostering a deeper understanding of data analysis and manipulation. As participants navigate through this comprehensive learning experience, they not only sharpen their Python skills but also cultivate a problem-solving mindset essential for real-world applications.