w3resource

Python Exercises: Two strings contain three letters at the same index

Python String: Exercise-107 with Solution

Write a Python program to that takes two strings. Count the number of times each string contains the same three letters at the same index.

Sample Data:
(“Red RedGreen”) -> 1
(“Red White Red White) -> 8
(“Red White White Red”) -> 0

Sample Solution-1:

Python Code:

def test(text1, text2):
           ctr = 0
           for i in range(len(text1) - 1):
                       if text1[i:i+3] == text2[i:i+3]:
                                   ctr += 1
           return ctr
text1 ="Red"
text2 ="RedGreen"
print("Original strings:", text1,text2)
print("Check said two strings contain three letters at the same index:")
print(test(text1, text2))
text1 ="Red White"
text2 ="Red White"
print("Original strings:", text1,text2)
print("Check said two strings contain three letters at the same index:")
print(test(text1, text2))
text1 ="Red White"
text2 ="White Red"
print("Original strings:", text1,text2)
print("Check said two strings contain three letters at the same index:")
print(test(text1, text2))

Sample Output:

Original strings: Red RedGreen
Check said two strings contain three letters at the same index:
1
Original strings: Red White Red White
Check said two strings contain three letters at the same index:
8
Original strings: Red White White Red
Check said two strings contain three letters at the same index:
0

Flowchart:

Flowchart: Two strings contain three letters at the same index.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Python Code:

def test(text1, text2):
           return sum([1 for i in range(len(text1)-1) if text1[i:i+3] == text2[i:i+3]])
text1 ="Red"
text2 ="RedGreen"
print("Original strings:", text1,text2)
print("Check said two strings contain three letters at the same index:")
print(test(text1, text2))
text1 ="Red White"
text2 ="Red White"
print("Original strings:", text1,text2)
print("Check said two strings contain three letters at the same index:")
print(test(text1, text2))
text1 ="Red White"
text2 ="White Red"
print("Original strings:", text1,text2)
print("Check said two strings contain three letters at the same index:")
print(test(text1, text2))

Sample Output:

Original strings: Red RedGreen
Check said two strings contain three letters at the same index:
1
Original strings: Red White Red White
Check said two strings contain three letters at the same index:
8
Original strings: Red White White Red
Check said two strings contain three letters at the same index:
0

Flowchart:

Flowchart: Two strings contain three letters at the same index.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: Replace repeated characters with single letters.
Next Python Exercise: Hash elements.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting sorted iterable (can sort by "compare" function):

>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]

>>> sorted(a,key=abs)
[1, 2, -3]