# Python program to find the string weight

In this article, the given task is to find the total string weight. To calculate the string weight, we have converted the given strings into the lower form. Considering the weight of the characters, we have taken a=1, b=,2 and so up to z=26. In this Python article, using two different examples, the approaches to find the given string’s weight are given. In the first example, given characters in a string are fetc, hed, and then their respective weight is added to the updated weight. In example 2, first the frequency of occurrence of a given character in the string is calculated, and then that frequency is multiplied by the respective character weight and then all such component weights are added together to give the final result.

## Example 1: Finding the String Weight using iteration and adding the character weights.

### Algorithm

Step 1 − First make atoz = ' abcdefghijklmnopqrstuvwxyz'.

Step 2 − We will use atoz.index() function to get the weight numbers, for example, here space ‘ ’ will have 0 value and b will have 2 value, and so on.

Step 3 − Now specify the given string for which the String weight is to be calculated.

Step 4 − Iterate through the given string to fetch the characters one by one.

Step 5 − Find the position value (weight value) of the character in atoz.

Step 6 − Update the String weight by adding the weight value of the char.

Step 7 − Finally, print the total result in the end.

### Example

givenstr = 'this is a sample string'
def calculateWeight(teststr):
teststr = teststr.lower()
atoz = ' abcdefghijklmnopqrstuvwxyz'
weight = 0
for item in range(len(teststr)):
elem = teststr[item]
currweight = atoz.index(elem)
weight += currweight
print("This albhabet:",elem, ", alphabet weight:", currweight, ", Updated String Weight ", weight)
return weight
finalresult= calculateWeight(givenstr)
print("Final String Weight: ",finalresult)


### Output

This albhabet: t , alphabet weight: 20 , Updated String Weight  20
This albhabet: h , alphabet weight: 8 , Updated String Weight  28
This albhabet: i , alphabet weight: 9 , Updated String Weight  37
This albhabet: s , alphabet weight: 19 , Updated String Weight  56
This albhabet:   , alphabet weight: 0 , Updated String Weight  56
This albhabet: i , alphabet weight: 9 , Updated String Weight  65
This albhabet: s , alphabet weight: 19 , Updated String Weight  84
This albhabet:   , alphabet weight: 0 , Updated String Weight  84
This albhabet: a , alphabet weight: 1 , Updated String Weight  85
This albhabet:   , alphabet weight: 0 , Updated String Weight  85
This albhabet: s , alphabet weight: 19 , Updated String Weight  104
This albhabet: a , alphabet weight: 1 , Updated String Weight  105
This albhabet: m , alphabet weight: 13 , Updated String Weight  118
This albhabet: p , alphabet weight: 16 , Updated String Weight  134
This albhabet: l , alphabet weight: 12 , Updated String Weight  146
This albhabet: e , alphabet weight: 5 , Updated String Weight  151
This albhabet:   , alphabet weight: 0 , Updated String Weight  151
This albhabet: s , alphabet weight: 19 , Updated String Weight  170
This albhabet: t , alphabet weight: 20 , Updated String Weight  190
This albhabet: r , alphabet weight: 18 , Updated String Weight  208
This albhabet: i , alphabet weight: 9 , Updated String Weight  217
This albhabet: n , alphabet weight: 14 , Updated String Weight  231
This albhabet: g , alphabet weight: 7 , Updated String Weight  238
Final String Weight:  238

## Example 2: Finding the String Weight using character weight and occurrence formula

### Algorithm

Step 1 − First make a dictionary called charweight= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, …………. Upto 'z': 26}

Step 2 − Now specify the given string for which the String weight is to be calculated.

Step 3 − Find the frequency occurrence of characters in the given string.

Step 4 − Iterate through the char weight dictionary and find the weight value for each character in the given string.

Step 5 − Multiply the frequency of occurrence of a character with its weight.

Step 6 − Update the String weight by adding this calculated value.

Step 7 − Repeat this and print the total result at the end.

The Description for the terms used in the given formula

TotalWeight is the Total weight of the given test String.

N1, n2 means the characters occurring in the given test string

Occr(n1) means the occurrence of n1 in the given test string.

Weight(n1) means the character weight of the given character n1 in the charweight dictionary.

Here ‘*’ is being used as the multiplication operator for numbers

Here ‘+’ is being used as the addition operator for numbers

### The Formula Used

TotalWeight= (Occr(n1) * Weight(n1)) + (Occr(n2) * Weight(n2)) ….. so on

### Example

givenstr = 'this is a sample string'
charweight= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
WeightSum=0
occurFreq = {}
for i in givenstr:
if i in occurFreq:
occurFreq[i] += 1
else:
occurFreq[i] = 1

print("Char Weights: " , charweight)
print("Occurance: ", occurFreq)

for alphabetChar, alphabetCharCount in occurFreq.items():
print(alphabetChar, ":", alphabetCharCount)
for key in charweight.keys():
if key.find(alphabetChar) > -1:
#print(charweight[key]*alphabetCharCount)
WeightSum=WeightSum + charweight[key]*alphabetCharCount
#print(WeightSum)
print("This albhabet:",alphabetChar, ", alphabet Count:", alphabetCharCount, ",  alphabet Weight:", charweight[key], " Updated String Weight ", WeightSum)

print("Final String Weight: ", WeightSum)


### Output

Char Weights:  {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
Occurance:  {'t': 2, 'h': 1, 'i': 3, 's': 4, ' ': 4, 'a': 2, 'm': 1, 'p': 1, 'l': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1}
t : 2
This albhabet: t , alphabet Count: 2 ,  alphabet Weight: 20  Updated String Weight  40
h : 1
This albhabet: h , alphabet Count: 1 ,  alphabet Weight: 8  Updated String Weight  48
i : 3
This albhabet: i , alphabet Count: 3 ,  alphabet Weight: 9  Updated String Weight  75
s : 4
This albhabet: s , alphabet Count: 4 ,  alphabet Weight: 19  Updated String Weight  151
: 4
a : 2
This albhabet: a , alphabet Count: 2 ,  alphabet Weight: 1  Updated String Weight  153
m : 1
This albhabet: m , alphabet Count: 1 ,  alphabet Weight: 13  Updated String Weight  166
p : 1
This albhabet: p , alphabet Count: 1 ,  alphabet Weight: 16  Updated String Weight  182
l : 1
This albhabet: l , alphabet Count: 1 ,  alphabet Weight: 12  Updated String Weight  194
e : 1
This albhabet: e , alphabet Count: 1 ,  alphabet Weight: 5  Updated String Weight  199
r : 1
This albhabet: r , alphabet Count: 1 ,  alphabet Weight: 18  Updated String Weight  217
n : 1
This albhabet: n , alphabet Count: 1 ,  alphabet Weight: 14  Updated String Weight  231
g : 1
This albhabet: g , alphabet Count: 1 ,  alphabet Weight: 7  Updated String Weight  238
Final String Weight:  238

## Conclusion

We give here two different approaches to show how to find the String Weight of a given string. First, the characters used are fetched one by one from the given test string, then their respective weights are added. By repeating this process the final String weight is calculated. In example 2, first the character frequency in a string is found, then that frequency is multiplied by that character’s weight. This process is repeated with all the characters used in a given string and then the final String weight is calculated.

Updated on: 10-Jul-2023

134 Views 