Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 96

Python Exercise Example 96

Python 100 Examples Python 100 Examples

**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

)

print

ncount

The output of the above example is:


Please enter a string:
www.runoob.com
Please enter a substring:
runoob
1

Python 100 Examples Python 100 Examples