如何在Python2.7中从字符串中按顺序查找字母

如何在Python2.7中从字符串中按顺序查找字母,python,string,python-2.7,Python,String,Python 2.7,对于我在Python2.7中制作的项目,我必须编写一些东西,可以判断字母“I”和“a”是否出现在用户输入的字符串中。字母必须按顺序排列,但不必按顺序排列(中间可以有其他字母)。我怎样才能对它进行编码,以便它能够检测到字符串中的一组条件 到目前为止我所拥有的是 name = easygui.enterbox("string being searched"); term1 = "i"; number = name.find(term1) term2 = "a"; number = name.find

对于我在Python2.7中制作的项目,我必须编写一些东西,可以判断字母“I”和“a”是否出现在用户输入的字符串中。字母必须按顺序排列,但不必按顺序排列(中间可以有其他字母)。我怎样才能对它进行编码,以便它能够检测到字符串中的一组条件

到目前为止我所拥有的是

name = easygui.enterbox("string being searched");
term1 = "i";
number = name.find(term1)
term2 = "a";
number = name.find(term2)
if(number > 1):
    easygui.msgbox("message")
    bonus = True
else:
    bonus = False
。。。但它没有考虑字母的顺序。我已经问过很多类似的问题,但都没什么效果。

你会找到第一个字母最先出现的位置(以及是否出现),然后从该位置搜索第二个字母

str.find有一个可选的起始参数,可用于指定搜索第二个字母的起始位置。

您可以找到第一个字母第一次出现的位置(以及是否出现),然后从该位置搜索第二个字母


str.find有一个可选的开始参数,可用于指定搜索第二个字母的起始位置。

使用
find
string方法可获得子字符串第一次出现的索引。如果找不到,则返回-1

name = easygui.enterbox("string being searched")
term1 = 'i'
term2 = 'a'
position1 = name.find(term1)
position2 = name.find(term2)
if(position1 != -1 and position2 != -1 and position1 < position2):
    easygui.msgbox("message")
    bonus = True
else:
    bonus = False
name=easygui.enterbox(“正在搜索的字符串”)
term1='i'
term2='a'
位置1=名称。查找(term1)
位置2=名称。查找(term2)
如果(位置1!=-1和位置2!=-1和位置1<位置2):
easygui.msgbox(“消息”)
奖金=真
其他:
奖金=假

findstring方法提供子字符串第一次出现的索引。如果找不到,则返回-1

name = easygui.enterbox("string being searched")
term1 = 'i'
term2 = 'a'
position1 = name.find(term1)
position2 = name.find(term2)
if(position1 != -1 and position2 != -1 and position1 < position2):
    easygui.msgbox("message")
    bonus = True
else:
    bonus = False
name=easygui.enterbox(“正在搜索的字符串”)
term1='i'
term2='a'
位置1=名称。查找(term1)
位置2=名称。查找(term2)
如果(位置1!=-1和位置2!=-1和位置1<位置2):
easygui.msgbox(“消息”)
奖金=真
其他:
奖金=假

如前所述,find方法返回索引。不过,这可能很有用。不要使用在原始代码中被覆盖的
number
值,只需检查成员身份即可。我们可以在if语句中使用
in
来实现这一点

# set the value as a string just for easy use
name = 'i am super'

# check for membership of both "i" and "s"
if 'i' in name and 's' in name:

    # Now use the .find method to check the indexes and make sure they are in the order you want (i before a or in this case s).
    if name.find('i') < name.find('s'):

        # print the indexes just to 2x check
        print(name.find('i'))
        print(name.find('s'))

        # if both conditions are valid, print True, here's where you'd assign bonus to True
        print('True')
    else:
        # print or assign bonus to false.
        print('False')
#将值设置为字符串以便于使用
name=‘我是超级’
#检查“i”和“s”的成员资格
如果名称中的“i”和名称中的“s”:
#现在使用.find方法检查索引,并确保它们符合您想要的顺序(在a之前是i,在本例中是s)。
如果name.find('i')
如前所述,find方法返回索引。不过,这可能很有用。不要使用在原始代码中被覆盖的
number
值,只需检查成员身份即可。我们可以在if语句中使用
in
来实现这一点

# set the value as a string just for easy use
name = 'i am super'

# check for membership of both "i" and "s"
if 'i' in name and 's' in name:

    # Now use the .find method to check the indexes and make sure they are in the order you want (i before a or in this case s).
    if name.find('i') < name.find('s'):

        # print the indexes just to 2x check
        print(name.find('i'))
        print(name.find('s'))

        # if both conditions are valid, print True, here's where you'd assign bonus to True
        print('True')
    else:
        # print or assign bonus to false.
        print('False')
#将值设置为字符串以便于使用
name=‘我是超级’
#检查“i”和“s”的成员资格
如果名称中的“i”和名称中的“s”:
#现在使用.find方法检查索引,并确保它们符合您想要的顺序(在a之前是i,在本例中是s)。
如果name.find('i')
字符串中是否可以有多个“i”或“a”?这些情况的预期结果是什么?字符串中是否可以有多个“i”或“a”?这些案例的预期结果是什么?谢谢!它工作得很好,我感谢你的帮助。很高兴我能帮上忙!如果您满意,请接受正确的答案。谢谢!它工作得很好,我感谢你的帮助。很高兴我能帮上忙!如果您满意,请接受正确的答案。