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

Assignment 04

Uploaded by

adeelmshll
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 04

Uploaded by

adeelmshll
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Assignment Unit 5

Source Code of the program:


The program shown above is function that performs the following tasks, and I have used a

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

when they are needed (Willems, 2020).

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

display the number of characters from the left.

2. The function has two parameters; one string and the other one is an integer. Firstly, the

function displays the name of the user.

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).

string_object[start_position:end_position:step] A string slice always starts with

start_position and ends with end_position which is excluded (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

number excluding it.

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

well. At last, the count is printed.

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

reverses the characters.

Below is the output of the program:


References:

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

How to Reverse a String in Python. (n.d). Retrieved from

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.

You might also like