Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python help: I am trying to match 2 strings together by each character. For exam

ID: 638545 • Letter: P

Question

Python help: I am trying to match 2 strings together by each character. For example, I am supposed to match main input: ABCDEF and a user input of ABCCDF. I need to do this in a for loop. I know I can get (( for character in main_input )) to show all the character in the main input, but how would I go about making them match? I want to do something like (( if character 1 matches , then give a point/move to next letter ))

I just need to learn how to find the first/second/third character in a string and then do an if statement.

Explanation / Answer

Since, String is a set of characters. To retrieve each character, it is similar to calling a value of an array by its index value.

That is if userinput variable stores a string, then to retieve the value at postion 2, Just use the following statement,

           ch=userinput[1]

Here, the second character of the userinput variable value is present at index 1. As per the array's concept, the index value starts at 0.

Program code:

def main():

    maininput="ABCDEF"

    userinput=input("Enter a string in Capital letters: ")

   

    #to get the first, second, third characters in a string

    print("First character of a user input string is: ", userinput[0])

    print("Second character of a user input string is: ", userinput[1])

    print("Thrid character of a user input string is: ", userinput[2])

   

    print("*******************************************************")

    #logic to match each character in a string

    for i in range(len(maininput)):

        if maininput[i]==userinput[i]:

            print("Character ", userinput[i], "is matched with main input character ", maininput[i],".")

        else:

            print("Character ", userinput[i], "is not matched with main input character ", maininput[i],".")

            break

if __name__ == "__main__":

    main()

--------------------------------------------------------------------------------------------------------------

Sample Output1:

sh-4.2# python3 StringCompare.py     

Enter a string in Capital letters: AB

CDEF                                 

First character of a user input strin

g is:  A                             

Second character of a user input stri

ng is:  B                            

Thrid character of a user input strin

g is:  C                             

*******************************************************                   

Character  A is matched with main input character  A                      

Character  B is matched with main input character  B                      

Character  C is matched with main input character  C                      

Character  D is matched with main input character  D                      

Character  E is matched with main input character  E                      

Character  F is matched with main input character  F                      

sh-4.2#                              

Sample Output2:

sh-4.2# python3 StringCompare.py     

Enter a string in Capital letters: AB

CCEF                                 

First character of a user input string is:  A                             

Second character of a user input string is:  B                            

Thrid character of a user input string is:  C                             

******************************************************                   

Character  A  is matched with main input character  A .                   

Character  B  is matched with main input character  B .                   

Character  C  is matched with main input character  C .                   

Character  C  is not matched with main input character  D .               

sh-4.2#