checking if a letter is present in a string in python

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers >>> 'N' in 'PYTHON' True >>> 'N' in 'STACK OVERFLOW' False >>> 'N' in 'python' # uppercase and lowercase are not equal False >>> 'N' in 'python'.upper() True Also, there is no need for a conditional statement when assigning to your flag . Ra

检查一个字母是否存在于python中的字符串中

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 >>> 'N' in 'PYTHON' True >>> 'N' in 'STACK OVERFLOW' False >>> 'N' in 'python' # uppercase and lowercase are not equal False >>> 'N' in 'python'.upper() True 此外,分配给您的flag时不需要条件语句。 而不是 flag = False if 'N' in your_string: flag = True 做 fl

how to match substring in a string using python

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers Check if a Python list item contains a string inside another string 11 answers 如果我正确理解你的问题,在这里你可以试试这个: players_list=['Cricket','PSL','IPL','t20','shahid afridi','aamer yamin'] cur.execute("SELECT tweet FROM tweets_data") for row in cur.fetchall(): if

如何使用python匹配字符串中的子串

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 检查一个Python列表项是否包含另一个字符串中的字符串11个答案 如果我正确理解你的问题,在这里你可以试试这个: players_list=['Cricket','PSL','IPL','t20','shahid afridi','aamer yamin'] cur.execute("SELECT tweet FROM tweets_data") for row in cur.fetchall(): if any(True for p in players_list if p.lower

How to check if a dict value contains a word/string?

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers This should work. You should use in instead of consists . There is nothing called consists in python. "ab" in "abc" #=> True "abxyz" in "abcdf" #=> False So in your code: if inst['Events'][0]['Code'] == "instance-stop": if '[Completed]' in inst['Events'][0]['D

如何检查字典值是否包含单词/字符串?

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 这应该工作。 您应该使用in ,而不是consists 。 有没有什么叫python consists 。 "ab" in "abc" #=> True "abxyz" in "abcdf" #=> False 所以在你的代码中: if inst['Events'][0]['Code'] == "instance-stop": if '[Completed]' in inst['Events'][0]['Description'] # the string [Complete

How to compare two string with some characters only in python

This question already has an answer here: Compare strings in python like the sql “like” (with “%” and “_”) 2 answers Does Python have a string 'contains' substring method? 13 answers You could use in to check if a string is contained in an other: 'toyota innova' in 'toyota innova 7' # True 'tempo traveller' in 'tempo traveller 15 str' # True If you only want to match the start of

如何比较两个字符串与只在python中的一些字符

这个问题在这里已经有了答案: 比较python中的字符串,如sql“like”(带有“%”和“_”)2个答案 Python是否有一个字符串'contains'substring方法? 13个答案 你可以用in来检查一个字符串是否包含在另一个字符串中: 'toyota innova' in 'toyota innova 7' # True 'tempo traveller' in 'tempo traveller 15 str' # True 如果你只想匹配字符串的开始,你可以使用str.startswith : 'toyota innova 7'.startswith('t

Check if a string has 2 characters next to each other

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers 你不能这样做吗? the_string = "SS in a dark room" print "SS" in the_string ## True

检查一个字符串是否有2个字符相邻

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 你不能这样做吗? the_string = "SS in a dark room" print "SS" in the_string ## True

How do I find out if one string is in another string in Python?

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers For finding one string in another, you may want to try the string find method. if b.find(a) != -1: # -1 will be returned when a is not in b do_whatever To ignore capitalizations, you may want to do something like: if b.lower().find(a.lower()) != -1: do_whatever Add

如何找出一个字符串是否在Python的另一个字符串中?

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 为了在另一个字符串中找到一个字符串,您可能需要尝试字符串查找方法。 if b.find(a) != -1: # -1 will be returned when a is not in b do_whatever 忽略大写字母,您可能需要执行以下操作: if b.lower().find(a.lower()) != -1: do_whatever 附加评论:当我输入这个内容时,自从我最初提供这个答案以来已

Test a String for a Substring?

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers if "ABCD" in "xxxxABCDyyyy": # whatever 除了使用“in”运算符(最简单)之外,还有其他几种方法 index() >>> try : ... "xxxxABCDyyyy".index("test") ... except ValueError: ... print "not found" ... else: ... print "found" ... not found find() >>> if "xxxx

测试一个字符串的子字符串?

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 if "ABCD" in "xxxxABCDyyyy": # whatever 除了使用“in”运算符(最简单)之外,还有其他几种方法 index() >>> try : ... "xxxxABCDyyyy".index("test") ... except ValueError: ... print "not found" ... else: ... print "found" ... not found find() >>> if "xxxxABCDyyyy".find("ABCD")

Running unittest with typical test directory structure

The very common directory structure for even a simple Python module seems to be to separate the unit tests into their own test directory: new_project/ antigravity/ antigravity.py test/ test_antigravity.py setup.py etc. for example see this Python project howto. My question is simply What's the usual way of actually running the tests? I suspect this is obvi

使用典型的测试目录结构运行unittest

甚至一个简单的Python模块的通用目录结构似乎是将单元测试分离到他们自己的test目录中: new_project/ antigravity/ antigravity.py test/ test_antigravity.py setup.py etc. 例如看到这个Python项目howto。 我的问题只是实际运行测试的常用方法是什么? 我怀疑这对除我以外的所有人都是显而易见的,但是您不能仅仅从测试目录运行python test_antigravity.py ,因为import antigravity py

What is a mixin, and why are they useful?

In "Programming Python", Mark Lutz mentions "mixins". I'm from a C/C++/C# background and I have not heard the term before. What is a mixin? Reading between the lines of this example (which I've linked to because it's quite long), I'm presuming it's a case of using multiple inheritance to extend a class as opposed to 'proper' subclassing. Is thi

什么是混合,为什么它们有用?

在“Programming Python”中,Mark Lutz提到了“mixins”。 我来自C / C ++ / C#背景,我以前没有听说过这个术语。 什么是混合? 阅读这个例子(我已经链接到,因为它很长)的行之间,我假设这是一个使用多继承来扩展一个类,而不是'正确'的子类。 这是正确的吗? 为什么我想要这样做,而不是将新功能放入子类中? 就此而言,为什么mixin / multiple inheritance方法比使用composition更好? 混合与多重继承分开

.py not required for packages in Python 3?

I am using Python 3.5.1. I read the document and the package section here: https://docs.python.org/3/tutorial/modules.html#packages Now, I have the following structure: /home/wujek/Playground/a/b/module.py module.py : class Foo: def __init__(self): print('initializing Foo') Now, while in /home/wujek/Playground : ~/Playground $ python3 >>> import a.b.module >>>

.py在Python 3中不需要包?

我正在使用Python 3.5.1。 我在这里阅读文档和包部分:https://docs.python.org/3/tutorial/modules.html#packages 现在,我有以下结构: /home/wujek/Playground/a/b/module.py module.py : class Foo: def __init__(self): print('initializing Foo') 现在,在/home/wujek/Playground : ~/Playground $ python3 >>> import a.b.module >>> a.b.module.Foo() initializing Foo <a.b