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_For Loop - Fatal编程技术网

Python 按递减长度打印字符串

Python 按递减长度打印字符串,python,string,for-loop,Python,String,For Loop,我是python的初学者,所以请耐心听我说 我正在尝试获得以下输出: 输入:apple 输出: apple appl app ap a 我所尝试的: b = raw_input("Enter the String") for n in reversed(range(len(b)-1): print b[0:n] 我在for循环中遇到语法错误。需要帮助。您在for循环的末尾缺少所需的) for n in reversed(range(len(b)-1)): #<- two par

我是python的初学者,所以请耐心听我说

我正在尝试获得以下输出:

输入:
apple

输出:

apple
appl
app
ap
a
我所尝试的:

b = raw_input("Enter the String")
for n in reversed(range(len(b)-1):
    print b[0:n]

我在for循环中遇到语法错误。需要帮助。

您在
for
循环的
末尾缺少所需的

for n in reversed(range(len(b)-1)): #<- two parenthesis before the colon

for n in reversed(范围(len(b)-1)):#在
for
循环的
末尾缺少一个必需的

for n in reversed(range(len(b)-1)): #<- two parenthesis before the colon

对于反向(范围(len(b)-1))中的n:#可以使用负索引并避免反转字符串

b = raw_input("Enter the String")
print b 
for i in range(1, len(b)):
    print b[:-i]

可以使用负索引并避免反转字符串

b = raw_input("Enter the String")
print b 
for i in range(1, len(b)):
    print b[:-i]

你试过数一数你的括号吗?好好看看你的回溯我想你不是真的想让我们跟你“裸体”。)你试过数一数你的括号吗?好好看看你的回溯我想你不是真的想让我们跟你“裸体”。)如果我需要从文本文件中读取字符串,我应该在上面的程序中进行哪些修改?您可以将第一行下面的代码放入函数中,并通过字符串调用它。如果我需要从文本文件中读取字符串,我应该在上面的程序中进行哪些修改?您可以将第一行下面的代码放入函数并通过字符串调用它。