CS 1101 – Programming Fundamentals
University of the People
Programming Assignment Unit 5
The following is a Python program where I will display my name, "Cherry Htun," perform the
specified operations on it, and count the number of vowels. Below is the technical explanation of
the code and its output (Downey, 2015)
Function Definition and Name Initialization:
I start by defining the main() function where all operations are enclosed. I store the name
"Cherry Htun" in the variable name.
User Input for Characters to Display:
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.
Reversing the Name:
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
reversing the order of characters.
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
iterations and string operations in Python programming.
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf