ItP3 - String Member Function Flashcards
(27 cards)
What are string member functions?
Built-in functions in languages like Python specifically designed to operate on strings, facilitating tasks like manipulation, searching, slicing, and formatting.
What are some examples of string member functions? - (5)
- upper()
- lower()
- title ()
- strip ()
- split()
What does string member function ‘upper()’ do?
The upper() method, converts all characters in a string to uppercase,
What does string member function ‘lower()’ do?
The lower() method, converts all characters in a string to lowercase,
What does string member function ‘title()’ do?
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 can you print a string in uppercase in Python and example? - (3)
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
What does string member function ‘strip’ do?
The strip() method, removes leading and trailing whitespace characters (such as spaces) from a string, ensuring clean and trimmed input.
How can you combine string member functions together? - (2)
You can chain string processing methods together.
For example: print(my_string.strip().lower())
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
What is the output of this code?
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! **
Why are string member functions useful? - (2)
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).
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)
- Utilising if statements
- Converting user input to lowercase
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)
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
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)
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.
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
What is the 4 possible output of this code? - (3)
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
What is the purpose of this provided code snippet? - (2)
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
How does the provided code snippet handle variations in user input? - (3)
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.
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)
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.
How might you use a while loop to keep going until someone gets the password right? (Think don’t code) - (2)
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.
Why might using a while loop to keep prompting for the password be a bad idea? - (2)
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.
Modify this code so it does the comparison in upper case:
What is output of code if you typed in : PENCIL?