
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check whether the Average Character of the String is present or not in Python
Suppose we have a string s that contains alphanumeric characters, we have to check whether the average character of the string is present or not, if yes then return that character. Here the average character can be found by taking floor of average of each character ASCII values in s.
So, if the input is like s = “pqrst”, then the output will be 'r' because the average of character ASCII values are (112 + 113 + 114 + 115 + 116)/5 = 570/5 = 114 (r).
To solve this, we will follow these steps −
- total := 0
- for each ch in s, do
- total := total + ASCII of ch
- avg := the floor of (total / size of s)
- return character from ASCII avg
Let us see the following implementation to get better understanding −
Example Code
from math import floor def solve(s): total = 0 for ch in s: total += ord(ch) avg = int(floor(total / len(s))) return chr(avg) s = "pqrst" print(solve(s))
Input
"pqrst"
Output
r
- Related Articles
- Python - Check whether a string starts and ends with the same character or not
- Check whether a string is valid JSON or not in Python
- Check whether a character is Lowercase or not in Java
- Check whether a character is Uppercase or not in Java
- Program to check whether one value is present in BST or not in Python
- How to check whether a character is in the Alphabet or not in Golang?
- How to Check Whether a String is Palindrome or Not using Python?
- Python program to check whether a given string is Heterogram or not
- Java Program to Check Whether a Character is Alphabet or Not
- C++ Program to Check Whether a Character is Alphabet or Not
- Check whether the frequencies of all the characters in a string are prime or not in Python
- Check whether the vowels in a string are in alphabetical order or not in Python
- Program to check the string is repeating string or not in Python
- Check whether the given number is Euclid Number or not in Python
- Check whether the given number is Wagstaff prime or not in Python

Advertisements