Skip to main content

Section 7.16 Write-code questions

Checkpoint 7.16.1.

Fix line 2 so that it prints “Hi” instead of “hi”.
Solution.
The capitalize() method returns a new string; it doesn't modify the original because strings are immutable. As a result, you need to assign the value of s1.capitalize() to s1. Fix line 2 so that it prints “Hi” instead of “hi”.
s1 = "hi"
s1 = s1.capitalize()
print(s1)
The capitalize() method returns a new string; it doesn't modify the original because strings are immutable. As a result, you need to assign the value of s1.capitalize() to s1.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=str-writecode-2 data-questionlist='str-ex-meowacq, str-ex-meowansw' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: str-ex-meowacq, str-ex-meowansw</p> </div> </div>

Checkpoint 7.16.2.

Write code to evaluate the length of the string “I like green eggs” and print it. It should print “The length is 17”.
Solution.
You can use the len() method to find the length of a string. Write code to evaluate the length of the string “I like green eggs” and print it. It should print “The length is 17”.
sentence = 'I like green eggs'
print('The length is ' + str(len(sentence)))
You can use the len() method to find the length of a string.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=str-writecode-4 data-questionlist='str-ex-countacq, str-ex-countansw' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: str-ex-countacq, str-ex-countansw</p> </div> </div>

Checkpoint 7.16.3.

Take the following Python code that stores this string: string = "X-DSPAM-Confidence: 0.8475". Use find and string slicing to extract the portion of the string after the colon character and then use the float function to convert the extracted string into a floating point number called num.
Solution.
The float() function ignores whitespace, so you can begin the slice at the space character after the colon or at the 0 – your choice! Take the following Python code that stores this string: string = "X-DSPAM-Confidence: 0.8475". Use find and string slicing to extract the portion of the string after the colon character and then use the float function to convert the extracted string into a floating point number called num.
string = "X-DSPAM-Confidence: 0.8475"
colonpos = string.find(':')
digit = string[colonpos + 1:]
num = float(digit)
The float() function ignores whitespace, so you can begin the slice at the space character after the colon or at the 0 – your choice!
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=str-writecode-6 data-questionlist='str-ex-ducksacq, str-ex-ducksansw' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: str-ex-ducksacq, str-ex-ducksansw</p> </div> </div>

Checkpoint 7.16.4.

Write a function numDigits that will return the number of digits in an integer n.
Solution.
Convert the integer to a string, then use the len() method. Write a function numDigits that will return the number of digits in an integer n.
def numDigits(n):
    n_str = str(n)
    return len(n_str)

# Check the function
print(numDigits(50))
print(numDigits(20000))
print(numDigits(1))
Convert the integer to a string, then use the len() method.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=str-writecode-8 data-questionlist='str-ex-nameacq, str-ex-nameansw' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: str-ex-nameacq, str-ex-nameansw</p> </div> </div>

Checkpoint 7.16.5.

Write a program that asks a user for their name and from the input prints the first letter of their name in lowercase.
Solution.
Use the input() function to get the user's input, then use indexing and the lower() method to print the first letter of their name in lowercase. Write a program that asks a user for their name and from the input prints the first letter of their name in lowercase.
prompt = "What is your name?"
name = input(prompt)
first = name[0]
lowerFirst = first.lower()
print(lowerFirst)
Use the input() function to get the user's input, then use indexing and the lower() method to print the first letter of their name in lowercase.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=str-writecode-10 data-questionlist='str-ex-loweracq, str-ex-loweransw' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: str-ex-loweracq, str-ex-loweransw</p> </div> </div>