Python Exercises: Two strings contain three letters at the same index
Python String: Exercise-107 with Solution
Write a Python program 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) -> 7
(“Red White White Red”) -> 0
Sample Solution-1:
Python Code:
def test(text1, text2):
ctr = 0
for i in range(len(text1) - 2):
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: 7 Original strings: Red White White Red Check said two strings contain three letters at the same index: 0
Flowchart:

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)-2) 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: 7 Original strings: Red White White Red Check said two strings contain three letters at the same index: 0
Flowchart:

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.
Python: Tips of the Day
Get Current Process Id:
import os os.getpid()
21423
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook