简体   繁体   English

Jupyter Notebook,Python3 打印功能:无输出,无错误

[英]Jupyter Notebook, Python3 print function: No output, No error

Refer to the CODE and OUTPUT below.请参阅下面的代码和输出。 There is no OUTPUT for the third print statement.第三个打印语句没有 OUTPUT。 An altered print statement in its place such as print(long_word[3:7]) gives output (elin).替代的打印语句(例如 print(long_word[3:7]))给出输出 (elin)。

# [ ] print the first 4 letters of long_word
# [ ] print the first 4 letters of long_word in reverse
# [ ] print the last 4 letters of long_word in reverse
# [ ] print the letters spanning indexes 3 to 6 of long_word in Reverse
long_word = "timeline"
print(long_word[:4])
print(long_word[3::-1])
print(long_word[3:7:-1])
print(long_word[-1:-5:-1])

OUTPUT输出

time
emit

enil

What gives?什么给? The situation of this question also raised in link below.这个问题的情况也在下面的链接中提出。 It is unaddressed as of now.目前还没有解决。

The slice operation in Python is [start:end:step] , when step=-1 , it represents get values in the reverse direction. Python 中的切片操作是[start:end:step] ,当step=-1 ,表示取反方向的值。

So when use print(long_word[3::-1]) , it is actually from index 3 to index 0 which is determined by reverse direction flag step=-1 .因此,当使用print(long_word[3::-1]) ,实际上是从索引 3 到索引 0,这是由反向标志step=-1确定的。 But when use print(long_word[3:7:-1]) , it represents from index 3 to index 7 which is not a reverse order and it is a collision.但是当使用print(long_word[3:7:-1]) ,它表示从索引 3 到索引 7 这不是相反的顺序,它是一个冲突。

If you want to print last four letters in reverse try the below code:如果要反向打印最后四个字母,请尝试以下代码:

long_word = "Characteristics" 

print(long_word[14:10:-1])     

Result: scit结果: scit

14 is a starting string index 14 是起始字符串索引
10 is an ending string index 10 是结束字符串索引
-1 is used to reverse the string one by one -1 用于将字符串一一反转

long_word = "timeline"

print(long_word[0:4])

print(long_word[3::-1])

print(long_word[-1:3:-1])

print(long_word[-3:-7:-1])

This is what I have tried, I think it is an answer of your question.这是我尝试过的,我认为这是对您问题的回答。

The correct code is:正确的代码是:

long_word = "timeline"
    print(long_word[:4])
    print(long_word[3::-1]) 
    print(long_word[-1:-5:-1])
    print(long_word[6:2:-1])

time
emit
enil
nile

Note that when reversing, you state the wanted ending index first, then the wanted start index - 1 second(except for the 0 index do not subtract one from it), like so:请注意,在反转时,您首先声明想要的结束索引,然后是想要的开始索引 - 1 秒(除了 0 索引不要从中减去一),如下所示:

long_word(wanted ending index: wanted start index - 1: -1) long_word(想要结束索引:想要开始索引 - 1:-1)

long_word = "timeline"
print(long_word[:4])
print(long_word[3::-1])
print(long_word[:3:-1])
print(long_word[6:2:-1])  

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM