66.Python中startswith和endswith的使用
定义模型的models.py,示例代码如下:
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = 'category'
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
category = models.ForeignKey('Category', on_delete=models.CASCADE, null=True)
def __str__(self):
return "<(Article: id: %s,title: %s, content: %s)>" % (self.id, self.title, self.content)
class Meta:
db_table = 'article'
1. startswith:大小写敏感的判断某个字段的值是否以某个值开始的。示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__startswith='hello')
print(articles)
print(articles.query)
return HttpResponse("success")
首先,查看数据库表中的数据如下:

打印出结果:
<QuerySet []>:返回的QuerySet为空。
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE BINARY hello%. 需要注意的是这里执行的sql语句为LIKE BINARY hello%,LIKE BINARY代表的是区分大小写,hello%代表的是以hello为开头,%代表的是后面可以匹配任意多个字符。
2.istartswith: 大小写不敏感的判断某个字段的值是否以某个值开始的,示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__istartswith='hello')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下:
<QuerySet [<Article: <(Article: id: 1,title: Hello, content: 你好)>>, <Article: <(Article: id: 2,title: Hello World, content: 大家好)>>]> : 查找出两条符合条件的文章。
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title **LIKE hello%: 需要注意的是,这里将查询条件翻译成了sql语句为:article.title LIKE hello%,这里的LIKE代表的是不区分大小写,hello%代表的是以hello开头,后面可以匹配任意多个字符。
3.endswith: 大小写敏感的判断某个字段的值中是否含有某个值开始。示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__endswith='world')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下:
<QuerySet []> 输出的结果为空的QuerySet。
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE BINARY %world :这里的查询条件被翻译article.title LIKE BINARY %world,LIKE BINARY代表的是区分大小写的判断,%world代表的是以world为结束,前面可以匹配任意多个字符,如果可以满足条件就会返回。
4.iendswith: 不区分大小写判断某个字段的值中是否含有某个值,示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__iendswith='world')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下所示:
<QuerySet [<Article: <(Article: id: 2,title: Hello World, content: 大家好)>>]>: 返回了一个满足条件的文章
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE %world: 需要注意的是,这里为 LIKE,代表的是不区分大小写的判断,并且以world结尾,world前面可以匹配任意多个字符,满足条件才会被返回。
66.Python中startswith和endswith的使用的更多相关文章
- python 中startswith()和endswith() 方法
startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...
- Python的startswith和endswith
做文本处理的时候经常要判断一个文本有没有以一个子串开始,或者结束.Python为此提供了两个函数: S.startswith(prefix[, start[, end]]) -> bool 如果 ...
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- python中strip、startswith、endswith
strip(rm)用来删除元素内的空白符: rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符 startswith.endswith用来查找开头或结尾条件的元素 例子: ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- Python: 字符串开头或结尾匹配str.startswith(),str.endswith()
问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...
- 在Javascript中使用String.startsWith和endsWith
在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...
- python startswith与endswith
如果你要用python匹配字符串的开头或末尾是否包含一个字符串,就可以用startswith,和endswith比如:content = 'ilovepython'如果字符串content以ilove ...
- python startswith和endswith
startswith判断文本是否以某个或某几个字符开始; endswith判断文本是否以某个或某几个字符结束; text = 'Happy National Day!' print text.star ...
随机推荐
- lambda表达式-很好---《转载》
Lambda表达式详解 前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1.0 ...
- Fiddler里面AutoResponder和mock用法
AutoResponder 1.找到对应图片请求的链接 2.操作步骤如下图: mock 使用场景:上一步骤模块没有完成,但是下一步模块完成了,需要使用到快速模拟,主要使用的地方就是测试接口
- css - flex 定义排列方向
flex-direction定义伸缩项目放置在伸缩容器的排列方向,对应有四个值: (1)row:从左到右或从右到左 (2)row-reverse:与row属性相反 (3)column:从上到下排列 ( ...
- Day6-T4
原题目 Describe:差分约束模板题吧...LG上竟然是省选+ code: #include<bits/stdc++.h> #define INF 214748364 using na ...
- 什么是IPFS?IPFS与区块链有什么关系
1.什么是IPFS? IPFS是Inter Planetary File System(星际文件系统)的缩写,是一个典型的点对点分布式文件系统, 旨在用同一个文件系统连接所有的计算设备.这时候有些小伙 ...
- C语言循环
C 练习实例1 #include<stdio.h> int main() { int i,j,k; printf("\n"); //此处巧妙的利用循环次数和四个相等的关 ...
- 036、Java中三目运算符的使用
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- PowerShell中执行.net类库
Powershell脚本一个比较强大的功能是可以直接调用.net类库(ps core能调用.net core类库),除了调用系统类库外,也可以调用自己编写的类库,从而扩充我们脚本的功能.本文这里简单的 ...
- Java中null的判断
Java中空指针的异常十分常见 if (name != null && !name.equals("")) { //do something } 或者 if (!& ...
- maven详解 之 pom.xml
Maven 一个项目管理工具 其作用就是用来管理jar 包的 maven的核心 pom.xml配置文件 <project xmlns="http://maven.apache ...