String CS Thinking Example 2
Follow the full solution, then compare it with the other examples linked below.
Example 2
mediumWhat is the result of concatenating 'Score: ' + STRING(95) + '%'?
Solution
- 1 Step 1: STRING(95) converts the integer 95 to the string '95'.
- 2 Step 2: Concatenation joins the three strings: 'Score: ' + '95' + '%'.
- 3 Step 3: Result: 'Score: 95%'.
Answer
'Score: 95%'
String concatenation joins strings end to end. When mixing numbers and strings, the number must be converted to a string first (type casting) to avoid type errors.
About String
A data type that represents a sequence of characters (text), enclosed in quotation marks. Strings support operations like concatenation (joining), slicing (extracting substrings), searching, and comparison, and can contain letters, digits, spaces, and special characters.
Learn more about String โMore String Examples
Example 1 easy
Given s = 'Hello World', what is: (a) LENGTH(s), (b) s[0], (c) s[6..10] (substring from index 6 to 1
Example 3 easyGiven name = 'Alice', what does name + ' is ' + STRING(15) + ' years old' produce?
Example 4 mediumGiven word = 'PROGRAMMING', what is the result of each operation? (a) LENGTH(word) (b) word[0] + wor