Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 7.17 Write-code questions
Activity 7.17.1 .
Fix line 2 so that it prints “Hi” instead of “hi”.
Solution .
“Hi”“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>
Activity 7.17.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 .
“I like green eggs”“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>
Activity 7.17.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 .
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>
Activity 7.17.4 .
Write a function
numDigits that will return the number of digits in an integer
n.
Solution .
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>
Activity 7.17.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 .
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>You have attempted
of
activities on this page.