Final challenge: take text apart and put it back together
After this: Predict, choose, fix and complete programs that work on text.
This challenge has five steps, and each step is scored on its own. You will predict what a slice and an action print, pick the slice that gives a piece of a word, fix a program that tries to change a string in place, complete the condition of a counting loop, and count the digits in a password. Every step shows the code in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Predict it
Predict it
A slice, an action and the length. What does this print?
word = "python"print(word[1:4])print(word.upper())print(len(word))String word = "python";System.out.println(word.substring(1, 4));System.out.println(word.toUpperCase());System.out.println(word.length());Which slice
The box word holds "notebook". Which line gives book?
Fix it
This program is meant to turn cat into bat and print bat. Line 2 fails. Which line should replace it?
word = "cat"word[0] = "b"print(word)Output
TypeError: 'str' object does not support item assignment
String word = "cat";word.charAt(0) = 'b';System.out.println(word);Output
error: unexpected type required: variable found: value
Fill the line
Complete the missing line so that the program counts how many times a appears in banana and prints 3. Write the whole line, including the colon at the endbrackets and the opening brace.
word = "banana"count = 0for ch in word: ____________________ # ← is this character an a? count = count + 1print(count)String word = "banana";int count = 0;for (char ch : word.toCharArray()) { ____________________ // ← is this character an a? count = count + 1; }}System.out.println(count);Count it
A password rule: how many digits are in the password? What does this print?
pw = "ab12"count = 0for ch in pw: if ch.isdigit(): count = count + 1print(count)String pw = "ab12";int count = 0;for (char ch : pw.toCharArray()) { if (Character.isDigit(ch)) { count = count + 1; }}System.out.println(count);- Predict what slices and actions print, pick the slice that gives a piece, fix a program that tries to change a string in place, and complete two counting loops.