Please choose code language:
How to find out multiple substrings in one string. python
This topic is assigned to Ghenadies
cloudytech43 2020 December 29 09:40

Please find out how many times the sub-strings "cen", "com", "der", "hen", and "ven" show in the text. The results need to come out separately, such as cen = 3 and etc.

I know how to find out how many times a single sub-string shows up. I can make a code for how many times a single sub-string can show up, but how do I change the code into finding multiple sub-strings.

Can someone show me how to do this problem?

I need to put the results into a list so that it turns out to be like:

Code:- 

list1 = ["cen", "com", "der", "hen","ven"]
#the first list has the sub-strings
list2 = []
#and I will append the results for each into list2 so that it ends up looking like
list2 = [1,3,5]
#So cen corresponds with 1 and etc. Also note that these are not the right values

Here is a successful code for finding how many times 1 sub-string will appear.

Code:-

string = "Cryptography is the practice and study of techniques for secure communication in the presence of third parties called adversaries. More generally, cryptography is about constructing and analyzing protocols that prevent third parties or the public from reading private messages; various aspects in information security such as data confidentiality, data integrity, authentication, and non-repudiation are central to modern cryptography. Modern cryptography exists at the intersection of the disciplines of mathematics, computer science, electrical engineering, communication science, and physics. Applications of cryptography include electronic commerce, chip-based payment cards, digital currencies, computer passwords, and military communications."
substring = "is"

count = string.count(substring)

# print count
print("The count is:", count)
bit.ly/2K70Axe
You must login to post messages. Click here to log in.