Assignment 04
Assignment 04
function to bundle the instructions. If the program performs a specific task and is used
repeatedly, it is a better option to use a function since functions are self-contained and be called
1. The program prompts the user to enter their name and stores that in a string variable
called name and prompts the user again to enter a number. This number is later used to
2. The function has two parameters; one string and the other one is an integer. Firstly, the
3. Secondly, the function displays the characters of the string from the left using the number
that was entered by the user except the number itself. To sub-string a string, we can slice
the string. A string in Python is immutable so when we slice the string, Python creates a
new sub-string from the source and original string remains unchangeable (Pankaj, 2022).
Start_position is 0 and end_position shows the length of the string, and step is 1 by
default (Pankaj, 2022). To slice a string with a number and only display the n character
from left, we use [: number]. It displays the character from the left until it reaches that
4. The function also displays the number of vowels in the name. To do that, we need to use
a loop to iterate between the characters one by one. Moreover, we also need if condition
to check if a specific character is vowel or not. I have also declared a count variable that
adds one to it every time a vowel is found. So, using the loop, we check each character if
it is vowel or not. I have also included capital vowels since they should be counted as
5. Lastly, to reverse the characters of the string, we need slice string. [::-1] is the simplest
way to reverse the string (“How to Reverse a String in Python,” n.d.). This code only
Willems, K. (2020, January). Python Functions Tutorial. Datacamp. Retrieved March 7, 2024,
from https://www.datacamp.com/tutorial/functions-python-tutorial
Pankaj. (2022, August 4). Python slice string. DigitalOcean. Retrieved March 7, 2024, from
https://www.digitalocean.com/community/tutorials/python-slice-string
https://www.w3schools.com/python/python_howto_reverse_string.asp
1. This program prompts the user to enter their name and a number. The name is
stored in a string variable named name, and the number is stored in integer variable called
number.
2.