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

Programming_Assignment_Unit_5_Final

The document outlines a programming assignment focused on iterations and strings in Python, demonstrating three string operations: displaying the first n characters, counting vowels, and reversing a name. Each operation is encapsulated in a separate function for modularity and reusability. The program emphasizes best practices in coding and functional abstraction, referencing Downey's 'Think Python' for foundational concepts.

Uploaded by

uo
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)
2 views

Programming_Assignment_Unit_5_Final

The document outlines a programming assignment focused on iterations and strings in Python, demonstrating three string operations: displaying the first n characters, counting vowels, and reversing a name. Each operation is encapsulated in a separate function for modularity and reusability. The program emphasizes best practices in coding and functional abstraction, referencing Downey's 'Think Python' for foundational concepts.

Uploaded by

uo
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/ 6

1

Programming Assignment Unit 5

Md Khorshed Alam

University Of The People

CS 1101-01 Programming Fundamentals

Shanthini S

May 10, 2025

Programming Assignment Unit 5


2

In this unit, we explored the fundamental concepts of Iterations and Strings in Python. This

program demonstrates three string operations using a user’s input name:

1. Displaying the first n characters from the left

2. Counting the number of vowels in the name

3. Reversing the name

Each operation is broken down into a separate function to make the code more modular and

reusable.

Python Code:

# Function to display n characters from the left

def display_left_chars(name, n):

return name[:n]

# Function to count vowels in the name

def count_vowels(name):

vowels = 'aeiou'

name = name.lower() # Convert the whole string to lowercase once

count = 0

for char in name:

if char in vowels:

count += 1

return count

# Function to reverse the name

def reverse_name(name):

return name[::-1]

# Main program
3

name = input("Enter your name: ")

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

# Call and display results

print("First", n, "characters from the left:", display_left_chars(name, n))

print("Number of vowels in the name:", count_vowels(name))

print("Reversed name:", reverse_name(name))

Output:

Enter your name: Khorshed Alam

Enter number of characters to display from the left: 8

First 8 characters from the left: Khorshed

Number of vowels in the name: 4

Reversed name: malA dehsrohK

Explanation:

The revised program introduces modular programming using three well-defined functions:

display_left_chars(), count_vowels(), and reverse_name(). This structure aligns with the best

practices of breaking down a problem into smaller subproblems for clarity and maintainabil-

ity.

The function display_left_chars(name, n) uses string slicing to extract the first `n` characters

from the input name. It returns a substring starting from index 0 up to, but not including, in-

dex `n`.

The count_vowels(name) function counts how many vowels appear in the string. It iterates

over each character and checks its presence in the string 'aeiou'. It converts text to lowercase

first, ensuring case-insensitive counting.

The reverse_name(name) function returns the reversed string using Python’s slicing syntax

`[::-1]`, which effectively walks the string backward.


4

Each function handles one task, making the code cleaner and easier to debug or expand. For

instance, if the program later needed to ignore whitespace or count only distinct vowels, these

changes could be confined to the count_vowels() function without affecting others.

This approach not only demonstrates an understanding of iterations and strings (Chapters 7 &

8 of Downey, 2015), but also encourages early habits in functional abstraction — a valuable

skill in computer science.

Program Screen:
5

Output Screen:
6

References

Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green

Tea Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf

You might also like