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 ...
随机推荐
- 「USACO09FEB」改造路Revamping Trails
传送门 Luogu 解题思路 有点像这题,但是现在这道不能跑k遍SPFA了,会TLE. 那么我们就跑分层图最短路,然后就变成模板题了. 细节注意事项 别跑SPFA就好了. 参考代码 #include ...
- 学习SpringBoot零碎记录——配置应用URL名称
学习SpringBoot配置应用名称,结果发现坑 到网上找 到 https://blog.csdn.net/qq_40087415/article/details/82497668 server: p ...
- 多元线性回归算法python实现(非常经典)
对于多元线性回归算法,它对于数据集具有较好的可解释性,我们可以对比不过特征参数的输出系数的大小来判断它对数据的影响权重,进而对其中隐含的参数进行扩展和收集,提高整体训练数据的准确性.整体实现代码如下所 ...
- vscode点击ctrl键报错Request textDocument/definition failed.
现象 用vscode写java代码的时候突然出现,修复问题点击Ctrl时,输出窗口就打日志,报错Request textDocument/definition failed. 我百度唯一的有用线索就是 ...
- node - multer 加图片后缀
var multer = require('multer') var storage = multer.diskStorage({ destination: function (req, file ...
- LCA之tarjan离线
显然81篇题解是有点多了,不让我提交. 更为不好的是没有一篇详细的\(tarjan\)(不过我也不会写详细的). 不过\(tarjan\)并没有我们想象的那样难理解,时间也并不爆炸(巧妙的跳过难写二字 ...
- jackson处理json
原文连接 工具下载: jackson-core-2.2.3.jar 核心jar包,下载地址 jackson-annotations-2.2.3.jar 该包提供Json注解支持,下载地址 jackso ...
- UVA - 1626 Brackets sequence (区间dp)
题意:给定一个串,可能空串,或由'[',']','(',')'组成.问使其平衡所需添加最少的字符数,并打印平衡后的串. 分析:dp[i][j]表示区间(i,j)最少需添加的字符数. 1.递推. #in ...
- POJ 1852:Ants
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11754 Accepted: 5167 Description ...
- Java8 Stream分组
//根据排课id分组 Map<Integer, List<Schedule4Homework>> idSchedule4HomeworksMap = schedule4Home ...