Python Exercise Example 96
**Problem:** Count the number of times a substring appears in a string.
**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
)
ncount
The output of the above example is:
Please enter a string:
www.runoob.com
Please enter a substring:
runoob
1
Python 100 Examples