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:


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.


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)
THOMS 2021 July 13 08:47
Use a for loop to iterate through a list of substrings. Within the for loop, check if each substring is in the string using the in keyword syntax substring in string , and append the resulting boolean to a new list.
kingmilo 2022 April 23 06:35
bubagames 2022 May 24 10:26
Can you explain more?
Sarajhones 2022 June 08 11:05
mosesakki 2022 August 18 11:00
homeshifting 2022 August 20 04:43
angelicajohn 2022 September 06 11:57
I am very happy to find this site. I wanted to thank you for this immense read!! Good written. 
ajaxrewiewca 2022 September 13 11:01
BlueLenovo027 2022 September 21 03:07
i'm still not get it, can you share another round?
rife09ka 2022 September 24 18:13
pilardhana 2022 October 09 19:24
You must login to post messages. Click here to log in.