+1 (315) 557-6473 

Python Program to Manipulate Strings Assignment Solution.


Instructions

Objective
Write a Python assignment that challenges students to develop a program for manipulating strings. The goal of the assignment is to enhance students' understanding of string operations, such as concatenation, slicing, and searching. This assignment can be an excellent opportunity for students to practice their Python programming skills while working with strings.

Requirements and Specifications

program to manipulate strings in python
program to manipulate strings in python 1

Source Code

CODE 1

def CapitalizeSentence(sentence):

    # First, convert the string to a list of sentences

    sentences = sentence.split(".")

    # Create a list to store the new sentences

    new_sentences = list()

    # Now, loop through the sentences and capitalize each one

    for i in range(len(sentences)):

        sentence = str(sentences[i])

        if len(sentence) > 0:

            # The sentence can contain empty spaces at the beginning, so remove them

            sentence = sentence.strip()

            sentence = sentence[0].upper() + sentence[1:]

            new_sentences.append(sentence)

    # Now, build the new sentence

    new_sentence = '. '.join(new_sentences)

    return new_sentence

if __name__ == '__main__':

    # Ask for string

    sentence = input("Enter sentence: ")

    # Capitalize

    new_sentence = CapitalizeSentence(sentence)

    # Print

    print(new_sentence)

CODE 2

# Read file

with open('StuGrades.txt') as f:

    lines = f.readlines()

    """

        We know that the lines in the document are: name, grade, name, grade, etc..

        So, if we have n lines, there are n/2 names and n/2 grades

    """

    # The following lists are parallel, which mean that the grade at index i is for the name at index i

    names = list() # list to store names

    grades = list() # list to store grades as integers

    # Get number of lines

    n = len(lines)

    # Variable to store highest grade and the name of the person.

    # Set the values to the ones from the first person

    highest_grade_name = lines[0]

    highest_grade = int(lines[1])

    # Variables to store lowest grade and the name of the person

    # Set the values to the ones from the first person

    lowest_grade_name = lines[0]

    lowest_grade = int(lines[1])

    # Variable to store the average grade and the number of persons

    # Initialize with the value of the first person

    avg = float(lines[1])

    n_students = 1

    # Now read from lines 2 to n-2

    # We start at index 2 because we already used the grade from the first person

    for i in range(2, n-1, 2):

        name = lines[i].strip() # Use strip to remove \n

        grade = int(lines[i+1])

        avg += grade

        n_students += 1

        # Check if the current grade is highest

        if grade > highest_grade:

            highest_grade_name = name

            highest_grade = grade

        # Check if the current grade is lowest

        if grade < lowest_grade:

            lowest_grade = grade

            lowest_grade_name = name

    # Calculate average

    avg = avg / n_students

    # Display

    print(f"{highest_grade_name} got the highest grade with a score of {highest_grade}")

    print(f"{lowest_grade_name} for the lowest grade with a score of {lowest_grade}")

    print("The average of the class: {:.1f}".format(avg))