python输入字符串,检测lo是否包含其中,如果是返回开始的索引值,否则返回负一
s = input()
print(s.find("ol"))
编写一个函数,接收字符串参数,返回一个元组,‘‘hello World’’,元组的第一个值为大写字母的个数,第二个值为小写字母个数
代码如下,点个👍吧
#编写一个函数,接收字符串参数,返回字符串,一个元组,元组第一个值为大写字母的个数,第二个值为小写字母的个数
def number(string):
big_num = 0
small_num = 0
for i in string:
if i.isupper():#除了利用函数来判断大小写字母外,利用ASCII码范围判断也可以
big_num += 1
elif i.islower():
small_num += 1
#还可以在这里离加一个判断非字母的else
'''
else:
another += 1
'''
return big_num,small_num
if __name__ == '__main__':
string = input('请输入一个字符串:')
count = number(string)
print(count)
点个👍吧,秋梨膏
可以使用Python字符串的find方法来检查一个字符串是否包含子字符串"lo",如果包含,find方法会返回子字符串在原字符串中的起始索引,如果不包含,返回-1。代码如下:
# 定义一个字符串
s = 'hello world'
# 检查字符串s是否包含子字符串'lo'
if 'lo' in s:
# 获取子字符串'lo'在字符串s中的起始索引
index = s.find('lo')
print(index)
else:
print(-1)
输出为:
3
如果字符串不包含子字符串'lo',则输出为-1。