Python Forum
String slicing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: String slicing (/thread-27238.html)



String slicing - divyansh - May-30-2020

str1="divyansh tiwari" # this is just a string

var1=str1[-1:-16:1] # in this i know that the slicing will start at -1 index that is the last index and will end on the -16th index that is excluding -16th index but here i used a positive number for steps that is 1 and this kinda slicing is making my control to go beyond the indexes but it is not show a error i am fine with that but its also not showing any output i am fine with that too but lets look down to the var2
print(var1)
var2=str1[-1:16:1] # here same terminology is used but the stopping index in changed to +16 from -16 and this is showing me a output that is prompting me with the last "i" in the string str1
print(var2)

# my question is that why second one is showing a output ( if you say that slicing includes the start index then what happened with the var1 output it is also starting with the same index)


RE: String slicing - BitPythoner - May-30-2020

Can you show us a sample script using the python tool? We need a much clearer picture.


RE: String slicing - divyansh - May-30-2020

i don't know what you are asking for i am new here and i tried to post a picture here but it didn't work


RE: String slicing - menator01 - May-30-2020

Please use tags when posting code

In your first example, you are moving further to the right
In the second exmple you are moving to the left of the string.
Someone please correct me if I'm wrong


RE: String slicing - bowlofred - May-30-2020

(May-30-2020, 09:30 PM)divyansh Wrote: # my question is that why second one is showing a output ( if you say that slicing includes the start index then what happened with the var1 output it is also starting with the same index)

The second index must be "beyond" the first index to show anything. In other words, it must lie in the correct step direction from the first index..

In your first instance, you are stepping forward (step = 1), but the second index is rearward (-16 is more rearward than -1). Because of this, no portion is selected.

In the second case, you are stepping forward again, but now 16 is beyond -1 (the last index of the string is 14). So displaying index 14 (same as index -1) is allowed.

These are all equivalent
# First index is in range, second index is in step direction.
>>> str1[14:100]
'i'
>>> str1[-1:100]
'i'
And these are equivalent
# First index is in range, second index is not in step direction.
>>> str1[14:1]
''
>>> str1[14:-16]
''
>>> str1[-1:-16]
''



RE: String slicing - ibutun - May-31-2020

Is this something you want?
list1 = list(range(14,0,-1))
list2 = list(range(0,-17,-1))
a = list1 + list2
print(list1)
print(list2)
print(a)
Output:
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] [0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16] [14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16]

or like this?
print(*range(14,0,-1))
print(*range(0,-17,-1))
print(*range(14,-17,-1))
Output:
14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16



RE: String slicing - pyzyx3qwerty - May-31-2020

(May-30-2020, 09:30 PM)divyansh Wrote: str1="divyansh tiwari" # this is just a string

var1=str1[-1:-16:1] # in this i know that the slicing will start at -1 index that is the last index and will end on the -16th index that is excluding -16th index but here i used a positive number for steps that is 1 and this kinda slicing is making my control to go beyond the indexes but it is not show a error i am fine with that but its also not showing any output i am fine with that too but lets look down to the var2
print(var1)
var2=str1[-1:16:1] # here same terminology is used but the stopping index in changed to +16 from -16 and this is showing me a output that is prompting me with the last "i" in the string str1
print(var2)

# my question is that why second one is showing a output ( if you say that slicing includes the start index then what happened with the var1 output it is also starting with the same index)

Please, post your code - you should have a first go at it - and also, use proper code tags not pictures, unless code is too big