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

Assignment p

Uploaded by

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

Assignment p

Uploaded by

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

Assignment: Iterations and Strings

Program Code and Explanation

# Display student's name

name = "John Doe" # Replace with your own name

# Accept input for number of characters to display

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

# Display the first n characters from the name

if n > len(name):

print(f"Your input exceeds the length of the name. Showing the entire name: {name}")

else:

print(f"The first {n} characters of your name are: {name[:n]}")

# Count the number of vowels in the name

vowels = "aeiouAEIOU"

vowel_count = sum(1 for char in name if char in vowels)

print(f"The number of vowels in your name is: {vowel_count}")

# Reverse the name


reversed_name = name[::-1]

print(f"Your name in reverse order is: {reversed_name}")

Technical Explanation

Displaying the Name and Taking User Input:

The program begins by defining a string variable name with a placeholder name ("John Doe"). The user can

replace this with their own name.

The input() function is used to take a number, n, from the user, which specifies how many characters from the

left of the name should be displayed. The input is converted to an integer using int().

Displaying n Characters from Left:

The program checks if the value of n is greater than the length of the name using len(name). If n exceeds the

length, the entire name is displayed.

If n is valid, the slice notation name[:n] retrieves the first n characters of the name.

Counting Vowels:

A string vowels containing all vowels (both uppercase and lowercase) is defined.
A generator expression sum(1 for char in name if char in vowels) is used to iterate through the characters in

name. For each character that matches a vowel, 1 is added to the total count. The result is stored in

vowel_count.

Reversing the Name:

The slicing technique name[::-1] is used to reverse the order of characters in the name. The step -1 starts from

the end of the string and iterates backward.

Output:

The results of all operations (n characters, vowel count, reversed name) are displayed using print() statements.

Example Output

Sample Input:

Enter the number of characters to display from the left of your name: 4

Sample Output:

The first 4 characters of your name are: John


The number of vowels in your name is: 3

Your name in reverse order is: eoD nhoJ

Descriptive Analysis

This program showcases three fundamental string operations in Python: slicing, counting, and reversing. The

slicing operation demonstrates how to extract portions of a string using indices. Counting vowels illustrates the

use of iteration to process each character in a string and conditional logic to match specific characters.

Reversing the string highlights Python's efficient slicing capabilities.

These techniques are foundational for text processing tasks such as string manipulation, data cleaning, or

encoding/decoding algorithms. The program is robust, handling edge cases like invalid input (n exceeding the

name length). It provides clear outputs, making it user-friendly and educational for beginners learning Python.

References

Downey, A. (2012). Think Python: How to Think Like a Computer Scientist. Green Tea Press. Retrieved from

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

You might also like