0% found this document useful (0 votes)
2 views

Programming 5

The Python program performs three operations on the string 'Vanessa': extracting the first 7 characters, counting the vowels, and reversing the string. It utilizes string slicing for extraction and reversal, and a for loop for counting vowels, demonstrating concepts from the book 'Think Python' by Allen B. Downey. The final output shows 'Vanessa' successfully reversed to 'assenav'.

Uploaded by

hrandretail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming 5

The Python program performs three operations on the string 'Vanessa': extracting the first 7 characters, counting the vowels, and reversing the string. It utilizes string slicing for extraction and reversal, and a for loop for counting vowels, demonstrating concepts from the book 'Think Python' by Allen B. Downey. The final output shows 'Vanessa' successfully reversed to 'assenav'.

Uploaded by

hrandretail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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/

You might also like