Python: Count characters at same position in a given string as in English alphabet
Python String: Exercise-78 with Solution
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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/string/python-data-type-string-exercise-78.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics