Programming 5
Programming 5
This Python program performs three main operations on the string "Vanessa":
To achieve this, the program follows concepts from Chapter 7 (Iteration) and Chapter 8 (Strings)
of Think Python, 2nd Edition by Allen B. Downey.
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.
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).
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/