w3resource

Python: Count characters at same position in a given string as in English alphabet

Python String: Exercise-78 with Solution

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:

Python String: Count characters at same position in a given string as in English alphabet.

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:

Flowchart: Count characters at same position in a given string as in English alphabet

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://198.211.115.131/python-exercises/string/python-data-type-string-exercise-78.php