Write a Python Program to Prefix frequency in string List


The program is a python script that counts the number of strings in a list that start with a specified substring.

  • A list of strings "val" is defined with 4 strings.
  • The original list "val" is printed using the "print()" function.
  • A variable "sub" is defined with a substring "Tj".
  • A counter "res" is initialized to 0.
  • A for loop is used to iterate over each string in the list "val".
  • The "startswith()" method is used to check if each string in the list starts with the substring "sub".
  • If the current string starts with the substring "sub", the counter "res" is incremented by 1.
  • After the for loop, the final value of the counter "res" is printed, which represents the number of strings in the list that start with the specified substring.

Source Code

val = ["TjC","TjCpp","TjPython","Java"]
print("Original List : ",val)
sub = 'Tj'
res = 0
for e in val:
	if e.startswith(sub):
		res = res + 1
print ("Strings count with matching frequency : ",res)

Output

Original List :  ['TjC', 'TjCpp', 'TjPython', 'Java']
Strings count with matching frequency :  3

Example Programs