Python: Count characters at same position in a given string as in English alphabet
Count chars matching English alphabet position.
Write a Python program to count characters at the same position in a given string (lower and uppercase characters) as in the English alphabet.
Visual Presentation:
Sample Solution:
Python Code:
# Function to count characters at same position
def count_char_position(str1):
count_chars = 0
# Iterate through string
for i in range(len(str1)):
# Check if position matches ASCII value
if ((i == ord(str1[i]) - ord('A')) or
(i == ord(str1[i]) - ord('a'))):
count_chars += 1
return count_chars
# Get input string
str1 = input("Input a string: ")
# Print result
print("Number of characters of the said string at same position as in English alphabet:")
print(count_char_position(str1))
Sample Output:
Input a string: xbcefg Number of characters of the said string at same position as in English alphabet: 2
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to count how many characters in a string are in the same position as they appear in the English alphabet.
- Write a Python program to iterate through a string and compare each letter’s index with its position in the alphabet to count matches.
- Write a Python program to implement a function that returns the count of characters whose order in the alphabet equals their index in the string.
- Write a Python program to use ord() and list comprehension to check if each character's position in a string corresponds to its alphabetical order.
Python Code Editor:
Previous: Write a Python program to count number of non-empty substrings of a given string.
Next: Write a Python program to find smallest and largest word in a given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics