ItP3 - String Member Function Flashcards

1
Q

What are string member functions?

A

Built-in functions in languages like Python specifically designed to operate on strings, facilitating tasks like manipulation, searching, slicing, and formatting.

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

What are some examples of string member functions? - (5)

A
  1. upper()
  2. lower()
  3. title ()
  4. strip ()
  5. split()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does string member function ‘upper()’ do?

A

The upper() method, converts all characters in a string to uppercase,

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

What does string member function ‘lower()’ do?

A

The lower() method, converts all characters in a string to lowercase,

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

What does string member function ‘title()’ do?

A

The title() method, , converts the first character of each word in the string to uppercase and the rest to lowercase, effectively capitalizing the first letter of each word.

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

How can you print a string in uppercase in Python and example? - (3)

A

To print a string in uppercase in Python, you can use the upper() method.

For example:
my_string = ‘the cat Sat on The Mat.’
print(my_string.upper())

Output:
MY CAT SAT ON THE MAT

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

What does string member function ‘strip’ do?

A

The strip() method, removes leading and trailing whitespace characters (such as spaces) from a string, ensuring clean and trimmed input.

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

How can you combine string member functions together? - (2)

A

You can chain string processing methods together.

For example: print(my_string.strip().lower())

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

Write a code that stores string ‘the cat Sat on The Mat’ into variable called ‘my_string’

print string in upper case

print string in lower case

print string in title case

print string and get rid of ‘whitespace’ characters (like spaces)

print string and get rid of ‘whitespace’ characters (like spaces) and print lower case as well

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

What is the output of this code?

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

Exercise:

Change the following code so that it prints the string ‘All In Title Case’ and without the leading and trailing spaces.

Bonus points. Use an f-string to center the modified string in some asterisks like this ** Hello World! **

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

Why are string member functions useful? - (2)

A

facilitate cleaning up and modifying strings, including changing their case

In scenario in which string member functions are useful is when in cases where we need to compare strings ‘case-insensitively’ (in other words without caring whether something is upper- or lower-case).

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

Example of using string member functions to compare strings ‘case insentively’

Let’s imagine that we are writing a super-secret password system for a school district. Each time someone wants to get in they have to type the password (‘pencil’). But, to make things easier, we don’t want it to fail if they have the CAPS LOCK key on, or if they are German And Need To Capitalize Lots Of Words. Or if they have accidentally added a space or tab at the start of the string (YNiC screensaver unlock box - I’m looking at you!).

What is a potential solution to handle variations in input when verifying passwords, as described in the scenario? - (2)

A
  1. Utilising if statements
  2. Converting user input to lowercase
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Example of using string member functions to compare strings ‘case insentively’

Let’s imagine that we are writing a super-secret password system for a school district. Each time someone wants to get in they have to type the password (‘pencil’). But, to make things easier, we don’t want it to fail if they have the CAPS LOCK key on, or if they are German And Need To Capitalize Lots Of Words. Or if they have accidentally added a space or tab at the start of the string (YNiC screensaver unlock box - I’m looking at you!).

What is a potential solution to handle variations in input when verifying passwords, as described in the scenario? - Utilizing if statements - (2)

A

One approach to address variations in input when verifying passwords is to utilize if statements to check for different capitalization variations and account for leading or trailing spaces.

However, this approach can be cumbersome and require extensive code modification if the password changes

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

Example of using string member functions to compare strings ‘case insentively’

Let’s imagine that we are writing a super-secret password system for a school district. Each time someone wants to get in they have to type the password (‘pencil’). But, to make things easier, we don’t want it to fail if they have the CAPS LOCK key on, or if they are German And Need To Capitalize Lots Of Words. Or if they have accidentally added a space or tab at the start of the string (YNiC screensaver unlock box - I’m looking at you!).

What is a potential solution to handle variations in input when verifying passwords, as described in the scenario? - Converting user input - (2)

A

Alternatively, converting the user input to lowercase and stripping leading and trailing spaces before comparison with the target password of ‘pencil’ simplifies the verification process and ensures consistency, regardless of the input format.

avoiding the need for multiple if statements.

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

Coding Exercise - example of using string member functions to compare strings ‘case insenstively’

Write a code that

stores target string ‘pencil’ into variable called target_string

Get input from user of their password and stores into variable ‘user_input’

Converts user’s input into lower case and stores into user_input

If statement of if user_input is equal to target_string it welcomes them to seattle public school distict DATANET

If not, says Password denied

A
17
Q

What is the 4 possible output of this code? - (3)

A

Login with user password: pencil Welcome to the Seattle Public School District DATANET

Login with user password: Pencil Welcome to the Seattle Public School District DATANET

Login with user password: Effort Access Denied

18
Q

What is the purpose of this provided code snippet? - (2)

A

The purpose of the code snippet is to create a password authentication system that accommodates variations in capitalization.

By converting the user input to lowercase before comparison, the code ensures consistent authentication regardless of capitalization differences

19
Q

How does the provided code snippet handle variations in user input? - (3)

A

The code snippet handles variations in capitalization by converting the user input to lowercase using the lower() method before comparison with the target password.

This ensures that inputs such as ‘PENCIL’, ‘Pencil’, or ‘pencil’ are all treated as equivalent to the target password ‘pencil’, simplifying the verification process.

By ignoring differences in capitalization, the code ensures accurate password verification.

20
Q

Suggest one improvement to enhance the robustness of the code snippet in handling variations in input. Explain how this improvement would enhance the code’s functionality - (3)

A

One improvement to enhance the robustness of the code snippet is to strip leading and trailing whitespace from the user input using the strip() method.

This improvement ensures that any accidental spaces or tabs at the start or end of the input do not affect the comparison with the target password.

By removing whitespace, the code enhances its ability to accurately verify passwords, improving overall functionality and reliability in handling input variations.

21
Q

How might you use a while loop to keep going until someone gets the password right? (Think don’t code) - (2)

A

You can use a while loop to repeatedly prompt the user for input and compare it to the correct password. Inside the loop, you can use an if statement to check if the input matches the password.

If it does, you can break out of the loop; otherwise, the loop continues until the correct password is entered.

22
Q

Why might using a while loop to keep prompting for the password be a bad idea? - (2)

A

Using while loop means stuck in an infinite loop for password verification

If the loop doesn’t have proper exit conditions, it could potentially lead to an infinite loop, where the program keeps prompting for the password indefinitely.

23
Q

Modify this code so it does the comparison in upper case:

A
24
Q

What is output of code if you typed in : PENCIL?

A
25
Q

What modification was made to the provided code snippet compared to original?

A

In the modified code, both the tgt_string variable was changed to ‘PENCIL’, converting the target string to all uppercase, and the user_input variable was converted to uppercase using the upper() method instead of lowercase.

26
Q

Why is it important to convert the user input to uppercase in the code snippet?

A

Converting the user input to uppercase is essential for accurate verification of the user’s password.

This adjustment enables a comparison that ignores differences in capitalization, ensuring that variations in how the password is entered in terms of its capitalisation are accepted and verified against the uppercase format of the target string (‘PENCIL’).

27
Q

What is output of the code

A