Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为变量Python指定最大/最小字符数(字符串)_Python_String - Fatal编程技术网

为变量Python指定最大/最小字符数(字符串)

为变量Python指定最大/最小字符数(字符串),python,string,Python,String,代码中有一些法语,但不用担心,我想在这里做的唯一一件事就是添加一些东西,防止我在ch1中输入4以下的字符串,在ch2中输入至少1个字符。这段代码适用于我正在做的事情 ch1 = input("Entre the first chain:") ch2 = input('Enter the second chain:') resultat = 0 sub_len = len(ch2) for i in range(len(ch1)): if ch1[i:i+sub_len] == ch2

代码中有一些法语,但不用担心,我想在这里做的唯一一件事就是添加一些东西,防止我在ch1中输入4以下的字符串,在ch2中输入至少1个字符。这段代码适用于我正在做的事情

ch1 = input("Entre the first chain:")
ch2 = input('Enter the second chain:')
resultat = 0
sub_len = len(ch2)


for i in range(len(ch1)):
    if ch1[i:i+sub_len] == ch2:
        resultat += 1

print('Chaîne 1 saisie: {}'.format(ch1))
print('Chaîne 2 saisie: {}'.format(ch2))
print('Réponse: La chaîne 2 se retrouve {} fois dans la châine 1.'.format(resultat))

您必须测试自己输入字符串的长度,如果输入无效,则在循环中重复输入

其中一种方法是:

while True:
    ch1 = input("Entre the first chain: (minimum 4 chars)")
    if len(ch1) >= 4:
        break

while True:
    ch2 = input('Enter the second chain (minimum 1 char):')
    if len(ch2) >= 1:
        break