7.16. Write Code Questions

  1. Fix line 2 so that it prints “Hi” instead of “hi”.

    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”.

  2. Fix the code so that only “meow” is printed.

    The string slicing syntax in line 2 was incorrect. Remember, the final number in the slice is the index of the character after the final one you want in the string.

    Fix the code so that only “meow” is printed.

  3. Write code to evaluate the length of the string “I like green eggs” and print it. It should print “The length is 17”.

    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”.

  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.

  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.

    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.

  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?

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

    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.

  8. Write code to print out the statement “Hi my name is Bob and I am 2” 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.

  9. Write a program that asks a user for their name and from the input prints the first letter of their name in lowercase.

    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.

  10. Write a program that asks for 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