ItP3 - String Member Function Flashcards
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.