University of the People
CS 1101-01 Programming Assignment Unit 5
Instructor: Dr. Kwok Wing Cheung
Vanessa Van Gersie
Technical Explanation of the Program
This Python program performs three main operations on the string "Vanessa":
Extracting the first n characters from the left
Counting the number of vowels
Reversing the string
To achieve this, the program follows concepts from Chapter 7 (Iteration) and Chapter 8 (Strings)
of Think Python, 2nd Edition by Allen B. Downey.
1. Extracting First n Characters
The variable n is set to 7, which represents the total number of characters in "Vanessa". String
slicing (name[:n]) is used to extract the first n characters from the name. In Python, slicing
allows us to access a substring efficiently without using loops.
2. Counting Vowels Using Iteration
The function count_vowels(name) iterates through each character in the string. It checks if the
character belongs to the set "AEIOUaeiou", which includes both uppercase and lowercase
vowels. The function maintains a count variable, which is incremented each time a vowel is
found. This demonstrates the use of a for loop, a key concept from Chapter 7, which focuses on
iteration.
3. Reversing the String Using Slicing
The program reverses "Vanessa" using the slicing operation name[::-1]. The -1 step means
Python traverses the string from the end to the beginning, effectively reversing it. This is a
powerful and efficient string manipulation technique discussed in Chapter 8, which covers how
strings are immutable and can be accessed through indexing. Downey (2015).
Final Output: “Vanessa” is successfully reversed to “assenav”
References
Downey, A. B. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.).
O’Reilly Media. Retrieved from https://greenteapress.com/thinkpython2/