Q1

In this question, you'll write some coding that performs string manipulation. This is pretty much your warm-up.

A

What's your favorite positive number? Reassign the favorite_number variable with something that's at least larger than 0.


In [ ]:
favorite_number = -1

### BEGIN SOLUTION

### END SOLUTION

print("My favorite number is: " + favorite_number)

In [ ]:
assert favorite_number >= 0

B

Print out a famous quote! In the code below, fill out the string variables to contain the name of a famous person, and a quote that they said.


In [ ]:
famous_person = ""
their_quote = ""

### BEGIN SOLUTION

### END SOLUTION

print("{}, at age {}, said:\n\n\"{}\"".format(famous_person, favorite_number, their_quote))

In [ ]:
assert len(famous_person) > 0
assert len(their_quote) > 0

C

You're working late on a homework assignment and have copied a few lines from a Wikipedia article. In your tired stupor, your copy/paste skills leave something to be desired. Rather than try to force your mouse hand to stop shaking, you figure it's easier to write a small Python program to strip out errant whitespace from your copying-pasting.

Reassign each of the three variables (keeping their names the same, and their respective content otherwise unaltered) so that the trailing whitespace on both ends is removed.


In [ ]:
line1 = 'Python supports multiple programming paradigms, including object-oriented, imperative\n'
line2 = '  and functional programming or procedural styles. It features a dynamic type\n'
line3 = ' system and automatic memory management and has a large and comprehensive standard library.\n '

### BEGIN SOLUTION

### END SOLUTION

In [ ]:
assert line1[-1] != '\n'
assert line2[0] != line2[1] != ' '
assert line2[-1] != '\n'
assert line3[0] != ' '
assert line3[-1] != '\n'

D

You discover that there are numbers in the text you'd like to be able to parse out for some math down the road. After stripping out the trailing whitespace, convert the numbers to their proper numerical form. Assign them to the variables num1, num2, and num3 respectively.


In [ ]:
line1 = ' 495.59863 \n'
line2 = '\t134  '
line3 = '\n\t -5.4 \t'

num1 = -1
num2 = -1
num3 = -1

### BEGIN SOLUTION

### END SOLUTION

In [ ]:
assert num1 > 495 and num1 < 496
assert num2 == 134
assert num3 > -6 and num3 < -5

E

Take the number below, find its square root, convert it to a string, and then print it out. You must use the correct arithmetic operator for the square root, as well as the correct casting function for the string conversion. Put the result in the variable str_version and print that out.


In [ ]:
number = 3.14159265359

str_version = ""

### BEGIN SOLUTION

### END SOLUTION

In [ ]:
assert str_version == "1.7724538509055743"