Skip to main content

Section 7.16 Write Code Questions

Activity 7.16.1.

Fix line 2 so that it prints โ€œHiโ€ instead of โ€œhiโ€.

Activity 7.16.2.

Fix the code so that only โ€œmeowโ€ is printed.

Activity 7.16.3.

Write code to evaluate the length of the string โ€œI like green eggsโ€ and print it. It should print โ€œThe length is 17โ€. Store the length in a variable named length.

Activity 7.16.4.

Create a function named count that accepts a string and a letter as arguments, then returns the count of that letter in the string. For example, if the function call was count("banana", "a") it would return 3. Hint: use the count method.

Activity 7.16.5.

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.

Activity 7.16.6.

In Robert McCloskeyโ€™s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. The following loop tries to output these names in order. Of course, thatโ€™s not quite right because Ouack and Quack are misspelled. Can you fix it?

Activity 7.16.7.

Write a function numDigits that will return the number of digits in an integer n.

Activity 7.16.8.

Write code to print out the statement โ€œHi my name is Bob2โ€ using only string methods or string slicing. You must get every part of the new string from the given strings, not by using string literals. Name the final string statement.

Activity 7.16.9.

Write a program that asks a user for their name (store the input in a variable named name) and from the input prints the first letter of their name in lowercase (store it in a variable named lowerFirst).

Activity 7.16.10.

Write a program that asks for user input (store it in a variable named user_input) and prints their input in all lowercase, as well as the length of their string.
You have attempted of activities on this page.