简体   繁体   English

如何不使用内置函数或切片在python中查找字符串的子字符串

[英]How do you find a substring of a string in python using no built-in function or slicing

I'm trying to work on a basic python program that is to find a substring of a string in python but the challenge was I can't use built functions or slicing. 我正在尝试在一个基本的python程序上工作,该程序将在python中找到字符串的子字符串,但是挑战是我不能使用内置函数或切片。

MAINSTRING = raw_input('Enter a string : ')

print 'You entered %s' %MAINSTRING

isSubString = raw_input('Enter the substring : ')

print 'You entered %s' %isSubString

if isSubString in MAINSTRING:
    print isSubString + " is a substring of " + MAINSTRING

It works but I can't use in syntax which is the frustrating part. 它的工作原理,但我不能用in语法这是令人沮丧的。 I also know to use the slicing method in python but my challenge was to break it to the basics. 我也知道在python中使用切片方法,但是我的挑战是将其分解为基础。

Sorry for being so vague, but I just got a hint. 抱歉,我含糊不清,但我只是得到了一个提示。

The complete code consist of two 'for' loops, one for the string and one for the substring 完整的代码包含两个“ for”循环,一个用于字符串,一个用于子字符串。

To avoid anything other than for loops and a function you could do it like this although I have no idea why. 为了避免除for循环和函数外的其他操作,尽管我不知道为什么,但是可以这样做。 Also you will have to use in which is pointed out in the comments. 你也将不得不使用in其中指出了意见。 If a letter in the mainstring matches the first letter in the substring then add it to the temp string t , then go to the next letters in each and see if they match. 如果主字符串中的一个字母与子字符串中的第一个字母匹配,则将其添加到临时字符串t ,然后转到每个字母中的下一个字母,查看它们是否匹配。 If the temp string equals the substring then it exists and it will return. 如果临时字符串等于子字符串,则它存在并且将返回。

def substring(ss, s):
    x = 0
    t = ""
    for i in range(len(s)):
        if s[i] == ss[x]:
            t += ss[x]
            if t == ss:
                return s
            x+=1
        else:
            x = 0
            t = ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何找到python内置函数实现? - How do I find python built-in function implementations? 如何在python中找到内置函数的复杂性 - How to find a complexity of a built-in function in python 你如何研究python的内置方法的实现? - How do you investigate python's implementation of built-in methods? 使用迭代而非内置函数模拟Python str.find(substring) - Emulate Python str.find(substring) using iteration but not built-in functions 如何以字符串形式获取python中内置help()函数的输出? - How to get the output of the built-in help() function in python in a string? 如何从python中的给定数据中删除字符串(不使用任何内置函数)? - How to delete string from given data in python(without using any built-in function)? 如何在不使用内置 transpose() function 的情况下对 Python 中的二维数组进行转置? - How to do transpose of a 2D array in Python, without using the built-in transpose() function? 如何使用 exec() 在字符串的 function 中使用内置模块 - How to use built-in modules in a function of a string using exec() 在Python中使用字符串调用内置函数 - Call a built-in function with a string in Python 如何使用 Python 中的内置字符串方法去除文本中的空格? - How to strip whitespace in text using built-in string methods in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM