Tags » Strings

Chapter 3: Strings (Exercises & Answers)

Exercises and Answers

Exercise #1:

For each of the following expressions, what value will the expression give? Verify your answers by typing the expressions into the Python shell.


Compress String

Write a function that compresses recurring characters for a given string. For example, aaabbbbccccdd returns a3b4c4d2.

The code proceeds as follows:


Permutation Test

Write a function to test if two strings are permutations of each other. As in the uniqueness check function, the key to this problem is recognizing there are 8 bytes per character resulting in 256 possible integer values for lower and uppercase characters. 180 more words


String Reversal

Write a function to reverse a string. We will go through half the string swapping pairs of characters each time.

The code proceeds as follows: 107 more words


Uniqueness Test

Write a function that checks whether or not all characters in a string are unique. The key idea is that each character in a string is 8 bytes resulting in a total of 26 (characters in the alphabet) * 2 (upper and lower cases) * 8 (bytes) = 256 possible integer values to represent a character. 192 more words



Strings are objects representing a sequence of characters. A great reference for strings can be found here.