Write a Python program to count number of non-empty substrings of a given string
The program takes an input string from the user and calculates the total number of possible substrings in the input string. Here's a step by step explanation of the code:
- The first line takes the input string from the user and stores it in the "str" variable.
- The next line calculates the length of the input string and stores it in the "len" variable.
- The next line calculates the total number of possible substrings in the input string using the following formula: (length of the string) * (length of the string + 1) / 2. The result is stored as an integer.
- The final "print" statement outputs the total number of possible substrings in the input string to the console.
Source Code
str = input("Input a string: ")
len = len(str);
print("Number of substrings:",int(len * (len + 1) / 2))
Output
Input a string: Joes
Number of substrings: 10