String manipulation, casting & ASCII arithmetic
Computer Science · Programming fundamentals
Question 1 of 10
0ASCII: 'A' = 65, 'a' = 97, '0' = 48. What does the Python expression `ord('r')` return?
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
String manipulation, casting & ASCII arithmetic explained
A string is a sequence of characters, such as a word or sentence. Common string operations are finding the length, joining strings together (concatenation), extracting a part of a string (a substring), and changing the case of letters. Each character is stored as a number, and the ASCII code is the standard that assigns these numbers. In ASCII, capital A is 65, lower case a is 97, and the digit 0 is 48. Capital letters and lower case letters are therefore 32 apart, which means changing case is a simple arithmetic step.
Casting means converting a value from one data type to another. Converting the string "42" into the integer 42 is casting a string to an integer, and converting the number 42 back into text is casting an integer to a string. Casting is needed when a program reads text from a user but has to perform arithmetic on it.
Arithmetic on ASCII codes is useful for many tasks. To convert a digit character to its value, subtract 48. To shift a letter along the alphabet, convert it to an index from 0 to 25, add the shift, take the remainder after dividing by 26 to wrap around the alphabet, and convert back to a letter.
Worked example
Use a Caesar shift of 3 to encode the letter Y.
- The ASCII code of Y is 89, so its index in the alphabet is .
- Add the shift and wrap: .
- Index 1 is B, so Y shifts to B. The code 65 + 1 = 66 is the ASCII code for B.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
ASCII: 'A' = 65, 'a' = 97, '0' = 48. What does the Python expression
ord('N')return?Medium
ASCII: 'A' = 65, 'a' = 97, '0' = 48. What does the Python expression
ord('S') - ord('G')evaluate to?Hard
ASCII: 'A' = 65, 'a' = 97, '0' = 48. What does the Python expression
sum(ord(ch) for ch in "dog")evaluate to?