Python‎ > ‎str‎ > ‎

str.count()

http://www.adilwaseem.com/

Introduction
The returns with the counting the characters.

Syntax
str.count(x)

Parameters
x=string

Examples
The function calculated string 'turtle' in webpage in django.
STEPS for creating virtual environment, startproject, startapps, migration, & running server.
  • virtualenv [virtual environment name]
  • cd  [virtual environment name]
  • .\Scripts\activate
  • .\Scripts\deactivate
  • pip install django
  • python .\Scripts\django-admin.py startproject [project name]
  • cd [project name]
  • python manage.py runserver
  • python manage.py startapp [name of app]
  • python manage.py migrate
  • python manage.py runserver
 After virtual environment, startproject, and startapp, the files structure look like this:


    In setting.py, put following intended app name, here it is newrequests.
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles','newrequests',
    )

    In urls.py, put following code: 
    from django.conf.urls import include, url
    from django.contrib import admin

    from newrequests.views import request1

    urlpatterns = [
        url(r'^newrequests/$', request1),
    ]

    In views.py, put following code:
    from django.http import HttpResponse
    import requests

    def request1(request):
        r=requests.get('http://www.pythonlake.com/Python/turtle')
        c=r.text
        ct=c.count('turtle')
        return HttpResponse(ct)
     
    Running the server at localhost:8000 through following command in CMD:
     python manage.py runserver



    >>> import requests
    >>> r=requests.get('http://www.pythonlake.com')
    >>> t=r.text
    >>> t.count('www.pythonlake.com')
    721 


    The method returns with:
     >>> "Python".count("p").__eq__(1)
    False
    >>> "Python".count("p").__eq__(0)
    True
    >>>

    The output are:
     >>> "Python Lake".count("Lake")
    1
    >>> "python Lake".count("Lake").__gt__(0)
    True
    >>> "python Lake".count("Lake").__eq__(1)
    True
    >>> "python Lake".count("Lake").__ne__(0)
    True
    >>>

    The returns is:
     >>> a_variable.count(str())==b_variable.count(str())
    False


    class.....: def....: str.count()
    The function find the number of vowels in the name through input() through str.count().
     class string_methods:
        def __init__(self,a,b):

             
            va=a.count("a")
            ve=a.count("e")
            vi=a.count("i")
            vo=a.count("o")
            vu=a.count("u")
            

            count_a=va+ve+vi+vo+vu

            print("Total vowels in your first name are:",count_a) 

            va=b.count("a")
            ve=b.count("e")
            vi=b.count("i")
            vo=b.count("o")
            vu=b.count("u")
            

            count_b=va+ve+vi+vo+vu

            print("Total vowels  in your last name :",count_b)


            print("Total vowels in your full name:", count_a+count_b)
            
            
    first_name=input("Please enter your first name: ")
    second_name=input("Please enter your second name: ")


    string_methods(first_name,second_name)


    >>> 
    Please enter your first name: adil
    Please enter your second name: waseem
    Total vowels in your first name are: 2
    Total vowels in your last name : 3
    Total vowels in your full name: 5
    >>>

    class....: def....: ...str.count
    The function allow the user to find word in their text.
     class method_text_finder():
        def __init__(self, a,b):

            self.a=str(a)
            find1=a.find(b)
            print("The word found in your text at position:",find1+1)

            count1=a.count(b)
            print("The total word found in your text:",count1)

    text=input("Please enter your text: ")
    find_word=input("Please enter your text:")

    method_text_finder(text,find_word)

    >>> 
    Please enter your text: Python Research Centre is located at Woodbridge Va
    Please enter your text:Woodbridge
    The word found in your text at position: 38
    The total word found in your text: 1
    >>> 


    Comments