python函数-获取一个字符串并仅返回特定字符

python函数-获取一个字符串并仅返回特定字符,python,function,Python,Function,抱歉,我是Python新手,有一个问题。如何创建一个函数,例如接受字符串26355,但只返回6?如中所示,如果给定给函数的字符串中有6,它将只返回一个值? 谢谢。您可以使用以下功能 def return_wanted_string(inp_str, wanted_str): return (wanted_str if wanted_str in inp_str else "Not found") 其中一种方法是- def search(input_string, matching_ch

抱歉,我是Python新手,有一个问题。如何创建一个函数,例如接受字符串26355,但只返回6?如中所示,如果给定给函数的字符串中有6,它将只返回一个值?
谢谢。

您可以使用以下功能

def return_wanted_string(inp_str, wanted_str):
    return (wanted_str if wanted_str in inp_str else "Not found")

其中一种方法是-

def search(input_string, matching_char):
    if input_string.index(matching_char) >= 0:
        return input_string
    return None
上述函数可以被称为

search("26355", "6")
在这种情况下,它将返回26355

def custom_function(string_word, specific_word):
    if specific_word in string_word:
        return specific_word
    else:
        return 'Nothing Find'

In [25]: custom_function('26343','6')
Out[25]: '6'
In [26]: custom_function('2343','6')
Out[26]: 'Nothing Find'

这是解决问题的最简单方法

你可以这样做-

def func(input_str, input_char):
    if input_char in input_str:
        return input_char
    return None
如果找到字符,则返回该字符 如果未找到字符,函数将返回None 您可以按如下方式使用Python命令:

print filter(lambda x: x == '6', '26355')
print filter(lambda x: x == '6', '263396')
这将给你:

6
66
这将有助于:


你能解释一下6是怎么来的吗?嘿,伙计们,谢谢你们的回复,我不知道是否有人回答了这个问题,但是如果给定的字符串有多个6呢?如何以字符串263396为例,仅从中返回字符串66?
number = "123456786960"
want = "6"
"".join([i for i in number if i == want])