-3

Use of string to print ?

St="hello to all my friends in python"
Print (St[1:3:-1])

IT print blank

GursimranSe
  • 99
  • 2
  • 8

3 Answers3

0

In print function you're slicing the string from 1st index to less than 3rd index. But you're using a negative step (which means reverse). So it won't print anything. If you use positive step then it will print el. Here is the corrected code:

print(St[1:3:1])
Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31
0

As you are reversing the string (-1 in the slicing), you should also treat the elements of the string reversed, so 3 comes before 1. Try the following:

print(St[3:1:-1])
Pedro Maimere
  • 204
  • 2
  • 8
0

I would do it like this:

s = "hello to all my friends in python"
print(s[1,2])
1cedsoda
  • 623
  • 4
  • 16