No, it doesn't It prints the String in reverse order. Yes, you can put that into a loop; start by writing it on paper with your screen turned off.
posted 1 week ago
what i know:
a string can be indexed 0 to string length eg: string "code" is [0:3]
-1 to -string length (from right to left [-1:-4]
make a for loop
for i in range (start count, to count to, to count by (default is 1?)
what i need to do:
print out a string in reverse
what i think should be done:
i need to create a string
myString = "I love CodeRanch"
create another empty string:
reverseString = ""
get the length of the string
stringLength = len(myString)
get the for loop to take indexes from the original string and put them in reverse to the new string
take the last index from the original string and put it in the first position of the new string
and so on
but i have no idea how t implement that in a loop
i dont even know if im going about solving this correctly
posted 1 week ago
in this code:
what is -i in the array doing, i dont really understand this
A positive index normally starts from the "left" side of the array or from index 0. A negative index will indicate an offset from a.length. I don't know if this makes sense so I'll give you an example:
If you have an array, a, that is 5 elements long with indices from 0-4, then a is the same as a[-5], and a is the same as a[-4], a is the same as a[-3], a is the same as a[-2], and a is the same as a[-1].
What don't you get? How a negative index works has already been explained. I would think you understand how basic arithmetic and addition/subtraction works so if you substitute values and do the math, you should be able to figure out what index values are used. I imagine you know how for loops work by now. And the end='' part just suppresses a line feed. Part of learning how to program is learning how to break down code into its component parts and then reassembling it again to understand how they all work together.
Why not give it another shot yourself and try to explain what your understanding is? That way we can see what exactly you're missing.