CS 1101 Programming Assignment Unit 5
CS 1101 Programming Assignment Unit 5
specified operations on it, and count the number of vowels. Below is the technical explanation of
I start by defining the main() function where all operations are enclosed. I store the name
To specify how many characters from the left they want to see, using the input() function which
captures their input and converts it to an integer with int(). This number is stored in the variable
n.
Displaying Characters:
Using slicing, name[:n], I extract and print the first n characters of the name. Slicing is a
powerful feature in Python that lets me easily manipulate parts of the string.
Counting Vowels:
I then count the vowels in name by comparing each character against the string vowels which
includes all vowels both in lowercase and uppercase. This is done using a generator expression
inside the sum() function, which is an efficient way to iterate over the string and count characters
conditionally.
To reverse the string, I use the slice name[::-1]. This slicing technique is a concise way to reverse
a string in Python, where [::-1] specifies to take the entire string and step backwards, thus
Execution Control:
The block if name == "main": ensures that main() is executed only when the script is run
directly. This is a common practice in Python to provide code modularity and allow the script to
be imported as a module without executing main().
Conclusion
In creating this program, I've applied fundamental Python concepts such as functions, slicing,
conditional expressions, and loops—each of which enhances our understanding of how to work
efficiently with strings and user inputs. This exercise not only demonstrates the practical
application of these concepts but also aligns with the educational goals of understanding
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf