strings.shannon_entropy
strings.shannon_entropy(string_to_calculate_entropy_for)
Description
A function that calculates the Shannon entropy of a string, and returns the result as a float. Low values of Shannon entropy indicates the provided string has a higher degree of predictability or certainty. For example, the same characters appearing many times in a string. High values of Shannon entropy indicate a higher degree of unpredictability or uncertainty. For example, a more even distribution of characters in a string where each character is equally likely to occur. High values of entropy can be useful for finding randomness in text; for example cryptographic, compressed, or randomly generated data.
Param data types
STRING
Return type
FLOAT
Code samples
This section contains examples that calculate the Shannon entropy of a string.
Example 1
Random string value.
strings.shannon_entropy("asdkjqweuriownvancxoql1273hagjga") = 4.375
Example 2
Empty string value returns a -1.
strings.shannon_entropy("") = -1
Example 3
Random string value with many characters.
strings.shannon_entropy("abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890") = 5.169925
Example 4
Low entropy string.
strings.shannon_entropy("google.com") = 2.646439