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? - (4)
- upper()
- lower()
- title ()
- strip ()
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).