Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String slicing
#1
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)
Reply
#2
Can you show us a sample script using the python tool? We need a much clearer picture.
Reply
#3
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
Reply
#4
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(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]
''
Reply
#6
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
Reply
#7
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 string slicing Luchano55 4 534 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,387 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  String slicing and loop iteration divyansh 9 4,619 Jun-07-2020, 10:29 PM
Last Post: divyansh
  string slicing help oli_action 2 2,150 Mar-22-2020, 09:57 AM
Last Post: oli_action
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 3,719 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  string slicing default value Uchikago 1 2,783 Jul-01-2019, 11:19 AM
Last Post: perfringo
  string slicing Uchikago 2 2,331 Jun-28-2019, 06:35 AM
Last Post: perfringo
  String slicing in python from reverse ift38375 1 2,358 Apr-29-2019, 06:58 AM
Last Post: perfringo
  String Slicing in List Comphrensions Patroclus72790 1 2,225 Mar-21-2019, 09:33 PM
Last Post: nilamo
  String slicing problem Ollew 4 2,742 Sep-08-2018, 08:07 PM
Last Post: Ollew

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020