Page 295 - CTS - CSA TP - Volume 2
P. 295

COMPUTER SOFTWARE APPLICATION - CITS




           Output:












           TASK 6: Write a program that takes a string as input and prints its hashed value using the hash function
           Code:
           string_to_hash = “hello”
           hashed_value = hash(string_to_hash)
           print(f”Hashed value of ‘{string_to_hash}’: {hashed_value}”)
           Explanation:

           1  string_to_hash = “hello”: This line initializes a string variable string_to_hash with the value “hello”.
           2  hashed_value = hash(string_to_hash): The hash() function is used to generate a hash value for the given
              string. It converts the string into a hash value, which is an integer representation of the string’s content. The
              resulting hash value is stored in the variable hashed_value.
           3  print(f”Hashed value of ‘{string_to_hash}’:  {hashed_value}”):  This line prints the original  string and its
              corresponding hash value. The f in print(f””) denotes an f-string, allowing variables to be directly inserted into
              the string. The output will show the original string and its associated hash value.
              In summary, this program demonstrates how to calculate the hash value of a string using the hash() function
              in Python. The hash value is a numeric representation of the input string’s content.
           Output:










           TASK 7: Write a program that calculates and prints the length of a given string

           Code:
           # 7.len
           str1=”Hello, World!”
           string_length = len(str1)
           print(f”Length of {str1} is: {string_length}”)

           Explanation:
           1  This exercise demonstrates the use of the len built-in function, which is used to determine the length of a
              sequence or collection. In this case, the input is a string str1 (“Hello, World!”), and the len function is applied
              to calculate the length of the string. The result is then printed to the console. The len function is versatile and
              can be used with various iterable objects like strings, lists, tuples, etc.












                                                           280
                              CITS : IT & ITES - Computer Software Application - Exercise 132
   290   291   292   293   294   295   296   297   298   299   300