0% found this document useful (0 votes)
16 views2 pages

Vowel Assignment

The document describes a Python program that defines a name, displays it, and allows the user to specify how many characters to show from the left. It includes a function to count the number of vowels in the name and displays the total count, as well as the reversed name. The program utilizes string slicing and basic control structures to achieve its functionality.

Uploaded by

cisse
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)
16 views2 pages

Vowel Assignment

The document describes a Python program that defines a name, displays it, and allows the user to specify how many characters to show from the left. It includes a function to count the number of vowels in the name and displays the total count, as well as the reversed name. The program utilizes string slicing and basic control structures to achieve its functionality.

Uploaded by

cisse
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

# Define the name

name = "Hassan Azeez"

# Display the name


print("Name:", name)

# Accept the number of characters to display from the left


n = int(input("Enter the number of characters to display from the left: "))

# Display n characters from the left


print("First", n, "characters:", name[:n])

# Define a function to count the number of vowels


def count_vowels(s):
# Initialize a counter for vowels
vowels_count = 0

# Iterate over each character in the string


for char in s:
# Check if the character is a vowel
if char.lower() in 'aeiou':
# Increment the vowels counter
vowels_count += 1

# Return the total count of vowels


return vowels_count

# Count the number of vowels in the name


vowels_count = count_vowels(name)

# Display the count of vowels


print("Number of vowels:", vowels_count)

# Reverse the name


reversed_name = name[::-1]

# Display the reversed name


print("Reversed name:", reversed_name)

Output:

Name: Hassan Azeez


Enter the number of characters to display from the left: 5
First 5 characters: Hass
Number of vowels: 4
Reversed name: zeA nezsaH
Explanation:

This program starts by defining the name "Hassan Azeez" and displaying it. Then, it prompts
the user to enter the number of characters to display from the left. The program uses Python's
string slicing feature (`name[:n]`) to extract and display the specified number of characters.

Next, the program defines a function `count_vowels(s)` to count the number of vowels in a
given string `s`. This function iterates over each character in the string, checks if the character
is a vowel (by converting it to lowercase and checking if it's in the string 'aeiou'), and
increments a counter if it is. Finally, the function returns the total count of vowels.

The program then calls this function with the name as the argument, stores the result in the
variable `vowels_count`, and displays this count.

Finally, the program reverses the name using Python's string slicing feature (`name[::-1]`) and
displays the reversed name.

Sources:

- "Think Python: How to Think Like a Computer Scientist" by Allen B. Downey (Chapters 7-
8)
- Python documentation (https://www.python.org/doc/)

You might also like