Python Exercise Example 96
Problem:Count the number of occurrences of a substring in a string.
Program analysis:None.
Example (Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
str1 = raw_input('Please enter a string:\n')
str2 = raw_input('Please enter a substring:\n')
ncount = str1.count(str2)
print ncount
The output of the above example is:
请输入一个字符串: www.example.com 请输入一个子字符串: example 1Other extensions
Python 100 Examples