Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Slicing a String gives no output in python

When i run this code it returns nothing

name = "Azura"
print(name[-1:-6])

Why does this happen ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because you’re slicing everything in the string from index -1 to index -6. In the case of the string name, after converting negative index to positive, that is index 5 to index 0. In otherwords, when you do print(name[-1:-6]), you’re doing print(name[5:0]), which obviously doesn’t work, because python slices left to right. Therefore, if you want print(name[0:5]), just do print(name[-6:-1]).

TLDR: BAD print(name[-1:-6]) GOOD print(name[-6:-1])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading