OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

slicing the last letter of a string in python while having both a start and stop-value?

  • Thread starter Thread starter Archangel071
  • Start date Start date
A

Archangel071

Guest
Is it possible to slice the last letter of a string in python while still having stop-value? I would like to create a loop to slice all the letters of a string individually from the back to the front, something like this:

Code:
str = "help"

for i in range(len(str)):
    letter = str[-(i+1):-i]
    print(letter, end=", ")

This would in an ideal world print "p, l, e, h, " but this approach does not work since it doesn't print the 'p'. I found that I could slice the 'p' by skipping the stop-value (letter = str[-1:]) but this would not be possible within the loop. I would then need to put the first slicing (of the p) outside of the loop which would be really annoying. Another solution could be to add a space in the string at the end but is there a better, "real", solution to this problem which allows me to keep both the loop and the string unaffected?
<p>Is it possible to slice the last letter of a string in python while still having stop-value? I would like to create a loop to slice all the letters of a string individually from the back to the front, something like this:</p>
<pre><code>str = "help"

for i in range(len(str)):
letter = str[-(i+1):-i]
print(letter, end=", ")
</code></pre>
<p>This would in an ideal world print "p, l, e, h, " but this approach does not work since it doesn't print the 'p'. I found that I could slice the 'p' by skipping the stop-value (<code>letter = str[-1:]</code>) but this would not be possible within the loop. I would then need to put the first slicing (of the p) outside of the loop which would be really annoying. Another solution could be to add a space in the string at the end but is there a better, "real", solution to this problem which allows me to keep both the loop and the string unaffected?</p>
 
Top