String Operatons Flashcards
How does the ‘+’ operator work when used with strings?
The ‘+’ operator will concatenate two or more strings into a single string.
How does the replace() method work?
this method takes two arguments, it then replaces all instances of the first argument with the second argument. Note this is a substring method.
How does the find() method work?
This method takes a string as an argument it then searches the string it was called on and returns the first index value where that substring is located.
How does the .format() method work?
This method can be used to interpolate variables into a string. Use a ‘f-string’ instead!
How does the basic split() method with no argumets work?
The split() method returns a list of substrings that has been split by a delimiter. If no argument is passed to the method the default delimiter is the “ “ space character.
How does the split method work with a delimiter as an argument?
Note that the delimiter must be passed as a string. The delimiter is not included in the list of substrings, the method returns a empty string if the string ends with the delimiter.
print(greatest_guitarist.split(‘a’))
output: [’s’, ‘nt’, ‘n’, ‘’”]
Note you can also split based on escape characters such as \n “newline” and \t “horizontal tab”
How does the join() method work?
The join() method is the opposite of the split() method. It uses a delimiter to join a list of strings.
‘delimiter’.join(list_you_want_to_join)
How do we create a csv from a list?
CSV stands for “Comma-Separated Values.” It is a simple and widely used file format for storing tabular data, such as spreadsheets or databases.
call the join() method on a “ ,” with a list of strings as the argument.
”,”.join(my_list_of_strings)
How does the strip() method work?
The strip() method with no arguments when called on a string removes white space from the start and end of the string. The method can also be called with a specific character which will be stripped from the start and end of the string.
Strings are immutable, what does this mean?
This means a string once created cannot be changed. Operations performed on strings such as slicing and concatenation create a new string.
What are the three main string methods that can change the case of a string?
‘.lower()’
‘.upper()’
‘.title()’
remember that strings are immutable so the result is a new string. The original string does not change.