Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Help
#1
For the following code:
my_string = "0123456789"
print(my_string[3])
I get the output '3', which I understand
but when I run:
print(my_string[3: 2: 4])
I get a blank space, I don't understand why I get a blank space as output, can anyone explain? I'll be extremely grateful
Reply
#2
See this post

https://python-forum.io/Thread-String-slicing--27238
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
String slicing specifies how to cut the string. Used in square brackets, the numbers are written in order of
[Position of the letter where it should start : Position where it should stop : Jumping]
(Important note : Slicing starts from 0 ,i.e., the first letter is 0, second is 1 etc.)
Let's take your example
my_string = "0123456789"
print(my_string[3])
Here, you have written 3, so it will display the the third element, which is 3
my_string = "0123456789"
print(my_string[3:5])
In the above line, the output will be 34. It won't display 345, as it just displays the first number and the numbers in between , not the ending one
my_string = "0123456789"
print(my_string[3:9:2])
Here, it will display 357 as the "2" in the end makes the string jump by 2 - if it was 1, it would be 345678
Note - default value is always set as 1

Now let's see your problem -
my_string = "0123456789"
print(my_string[3: 2: 4])
There is a problem in this :
  1. You can't reverse a string by doing the think you have done - to reverse you got to do like this
    str1 = "abcdefghijk"
    print(str1[::-1])
    So, these are the reasons your output is coming blank
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
#4
(Jun-01-2020, 10:08 AM)pyzyx3qwerty Wrote: There is only one element in between them it can't just jump by 4, can it?
Actually the problem is that end - 2 is less than start - 3, step - 4 has nothing to do with the problem, e.g.
>>> my_string = "0123456789"
>>> print(my_string[3:4:4])
3
start is included in the output, up to, but not including end.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jun-01-2020, 10:11 AM)buran Wrote:
(Jun-01-2020, 10:08 AM)pyzyx3qwerty Wrote: There is only one element in between them it can't just jump by 4, can it?
Actually the problem is that end - 2 is less than start - 3, step - 4 has nothing to do with the problem, e.g.
>>> my_string = "0123456789"
>>> print(my_string[3:4:4])
3
start is included in the output, up to, but not including end.

Thanks, I changed my post
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
#6
Now clear to me, thankyou so much all of you for great help!
I am really grateful for your time and efforts to help and guide me, salute!
Reply


Forum Jump:

User Panel Messages

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