Strings and Dictionaries Flashcards
Python allows us to concatenate using what operator?
+
This is super useful for taking you from one big string to a list of words
str.split()
[‘Pluto’, ‘is’, ‘a’, ‘planet!’]
Turns a string into a list of smaller strings, breaking on whitespace by default
str.split()
This puts a list of strings up into one long string, using the string it was called on as a separator
str.join()
‘/‘.join([month, day, year])
‘01/31/1956’
In a “format string” where the Python values we want to insert are represented by what?
{} curly braces
“{}, you’ll always be the {}th planet to me. “.format(planet, position)
Built-in Python data structure for mapping keys to values
What are dictionaries
The in operator tells us whether something is a what?
A key in the dictionary
‘Saturn’ in planet_to_initial
How can you access a collection of all the keys or all the values?
dict.keys() and dict.values()
Get all the initials, sort them alphabetically, and put them in a space-separated string.
‘ ‘.join(sorted(planet_to_initial.values()))
What does an item refer to?
A key value pair
Returns the string right justified in a string of length width
rjust()
def is_valid_zip(zip_code):
“””Return whether the input string is a valid (5 digit) zip code”””
return len(zip_code) == 5 and zip_code.isdigit()
What does (str.isdigit) do?
Returns True if string is digit, False otherwise
How do you create a dictionary?
{‘key1’:’value’,’key2’:123} curly braces
How would you grab a value in a key?
d = {‘key1’:’value’,’key2’:123}
d[‘key1’]
You have to call the actual key