6. Strings and dictionaries Flashcards
x = 'Pluto is a planet' y = "Pluto is a planet" x == y
True
or False
?
True
The following returns an error. How to fix it?
'Pluto's a planet!'
'Pluto\'s a planet!'
How would you do hello world on two separate lines?
Define variable and then print, using double quotes
hello = "hello\nworld" print(hello)
How to do hello world on two separate lines, using triple quotes?
triplequoted_hello = """hello world""" print(triplequoted_hello)
How do you do 2 separate print statments with words on the same line?
print("hello", end='') print("pluto", end='')
How would you slice the last 3 letters of this word?planet = 'Pluto'
planet[-3:]
How would you turn the following into a list of 5 items, one for each character, each one accompanied by an exclamation mark?planet = 'Pluto'
[char+'! ' for char in planet]
What’s the result of this? and why?planet[0] = 'B'
TypeError
B/c strings are immutable!
How would you turn this into upper case?claim = "Pluto is a planet!"
claim.upper()
How would you find the position of “uto” in the following…
claim = "Pluto is a planet!"
claim.index('uto')
How do you create a list of four items from the following?
claim = "Pluto is a planet!"
words = claim.split()
How would you split the following into 3 separate variables:
datestr = '1956-01-31'
output variables should be year, month, day
datestr = '1956-01-31' split_date = datestr.split(sep = "-") year, month, day = split_date
Join year, month, day into one string, separated by “/”
’/’.join([year,month,day])
How do you concantenate “what do you” with b = “mean” with “?”
d = “what do you” + b + “?”
b = 10
How would you say that “I am 10 years old”
Using str()
c = "I am " + str(b) + " years old"
b = 10
How would you say that “I am 10 years old”
Using .format method
c = "I am {} years old".format(b)
What will the following produce?
planet = "Jupiter" pluto_mass = 1.303 * 10**22 earth_mass = 5.9722 * 10**24 population = 52910390 "{} weighs about {:,.0f} kilograms ({:.3%} of Earth's mass). It is home to {:,} Plutonians.".format( planet, pluto_mass, pluto_mass / earth_mass, population, )
‘Jupiter weighs about 13,029,999,999,999,998,427,136 kilograms (0.218% of Earth’s mass). It is home to 52,910,390 Plutonians.’
What does this produce?
# Referring to format() arguments by index, starting from 0 s = """Pluto's a {0}. No, it's a {1}. {0}! {1}!""".format('planet', 'dwarf planet') print(s)
Pluto’s a planet.
No, it’s a dwarf planet.
planet!
dwarf planet!
How do you set up a dictionary with “one” as key and 1 as value, for first 3 numbers?
numbers = {'one':1, 'two':2, 'three':3}
How do you access the 2 value from the following?numbers = {'one':1, 'two':2, 'three':3}
numbers['two']
How do you append ‘eleven’ as a key to the following dictionary?numbers = {'one':1, 'two':2, 'three':3}
numbers['eleven'] = 11
How do you change value 1 to “chulent”numbers = {'one':1, 'two':2, 'three':3}
numbers[‘one’] = ‘chulent’
How do you produce the following dictionary from a list of planets?
{'Mercury': 'M', 'Venus': 'V', 'Earth': 'E', 'Mars': 'M', 'Jupiter': 'J', 'Saturn': 'S', 'Uranus': 'U', 'Neptune': 'N'}
Using a for loop
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'] planet_to_initial = {planet: planet[0] for planet in planets} planet_to_initial
How do you access the keys, values, and items in a dictionary (“dict”)?
dict.keys dicts.values dict.items