Python‎ > ‎

str

http://www.adilwaseem.com/

Definition
Create a new string object from the given object.

Syntax
As defined in sidebar.

Parameters
str=string


Examples

Through the QuerySet, the db is diplay through view.py. Only latest object is query in this this example.

mysite/settings
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles', 'myapp', 
)

models.py

from django.db import models

class ModelUrl(models.Model):
    url=models.URLField()

    def __str__(self):
        return self.url

myapp/views.py

from .models import ModelUrl
def URL_List(request):
    model = ModelUrl
    context_object_name = 'Url'
    url=ModelUrl.objects.latest('id')
    string=str(url)
    return HttpResponse(string)

mysite/ulrs.py

from django.conf.urls import include, url

from myapp.views import URL_List

urlpatterns = [
    url(r'^', URL_List),
]

python manage.py runserver

 




URLField is created through models and  viewed through index.html. 

Window CMD  commands 

  • virtualenv djangoschool
  • cd  djangoschool
  • .\Scripts\activate
  • pip install django
  • python .\Scripts\django-admin.py startproject mysite
  • cd mysite
  • python manage.py startapp myapp
  • python manage.py syncdb
  • python manage.py runserver




mysite/setting.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles', 'myapp',
)

mysite/urls.py

from django.conf.urls import include, url

from myapp.views import ViewUrl
urlpatterns = [
    url(r'^', ViewUrl)
]


myapp/models.py

from django.db import models

class ModelUrl(models.Model):
    url=models.URLField()

    def __str__(self):
        return self.url


myapp/forms.py

from django import forms
from .models import ModelUrl

class FormUrl(forms.ModelForm):
    class Meta:
        model=ModelUrl
        fields=['url']


myapp/views.py

from django.shortcuts import render

from django.shortcuts import HttpResponseRedirectHttpResponse
from .forms import FormUrl

def ViewUrl(request):
    form=FormUrl(request.POST)
    if form.is_valid():
        form.save(commit=False)
        form.save()
        return HttpResponseRedirect('/thanks/')
    else:
        form=FormUrl
    return render(request, 'myapp/index.html',{'form':form})

def response(request):
    return HttpResponse("Thanks")

myapp/index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<form  action="/thanks/" method="post">

    {%csrf_token%}

    {{form}}

    <input type="submit">

</form>

</body>
</html>

Localhost:8000



Database Query




Examples
The Response returns the union of frozenset x, y. 
from pyramid.response import Response

def frozenset1(request):
    x=frozenset({7,9,11,13})
    y=frozenset({5,10,15})
    z=frozenset.union(x,y)
    z=str(z)
    return Response('The union of frozenset is:' + z)
        
if __name__ == '__main__':
    config = Configurator()
    config.add_route('set', '/')
    config.add_view(frozenset1, route_name='set')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 1050, app)
    server.serve_forever()






The pyramid returns with the requests after finding the number of 'a' in www.pythonlake.com.
from wsgiref.simple_server import make_server
from pyramid.response import Response
import requests

def find(request):
    r=requests.get('http://www.pythonlake.com')
    text=r.text
    f=text.find('a')
    f=str(f)
    response = Response()
    response.text=f
    return response
    
if __name__ == '__main__':
    config = Configurator()
    config.add_route('find', '/')
    config.add_view(find, route_name='find')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 6500, app)
    server.serve_forever()






concatenation
The method concatenate the strings.
 >>> x="python"
>>> y="Lake Learning Center "
>>> x+y
'pythonLake Learning Center'
>>> 

str.lower()
The method return the string into lowercase and uppercase.
 >>> x="Python Lake"
>>> x.upper()
'PYTHON LAKE'
>>> x.lower()
'python lake'
>>>

str.capitalize()
The method capitalize the string.
 >>> "python lake".capitalize()
'Python lake'
>>> "Python Lake".upper()
'PYTHON LAKE'
>>> 

operator.is_(str,str)
The operator tested the string values.
 >>> operator.is_("Python", "Lake")
False
>>> operator.is_("Python", "Python")
True

a(str) is b(str)
The results are string return positive.
 >>> "Python" is "Python"
True
>>> 

operator.is_not 
The method return false on method operator is not
 >>> "Python Lake" is not "Python Lake"
False

__str__
The method convert the complex to string.
 >>> complex(2255888+0j).__str__()
'(2255888+0j)'

type(str.rjust())->str
The returns are :
 >>> type("Python".rjust(10))
<class 'str'>
>>> "Python".rjust(10).__eq__('    Python')
True
>>> len('    Python')
10
>>> len("Python".rjust(10))
10
>>> 


type(str)
The equal to:
 >>> "Python Research Centre".rfind("Centre")
16
>>> type("Python Research Centre".rfind("Centre"))
<class 'int'>
>>> str("Python Research Centre".rfind("Centre"))
'16'
>>> type(str("Python Research Centre".rfind("Centre")))
<class 'str'>

if (map(lambda function, string).__sizeof__.__gt__()
The method returns that a string size is greater than 22.
 str="python centre research"
if (map(lambda x: x, str)).__sizeof__().__gt__(22):
    print(str.__str__())
else:
    print("The string bytes size is greater than 22.")

>>> 
python centre research
>>> 

class....: def....: str.find()...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
>>> 


input(webbrowser.open_new())
webbrowser.open_new()
 import webbrowser

x=input("Please enter your web address: ")
y=str(x)

if y.startswith("y"):
    print("Access denied")
    webbrowser.open_new(y)

>>> 
Please enter your web address: yellow.com
Access denied

Subpages (39): View All
Comments