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"