String Variables: Printing individual characters


How to print last 3 (say) characters of a string?

My query:
If printing first 3(say) characters is-
name = “Python”

AND printing last character is-
name = “Python”

Then for last 3 characters in Reverse order-
name = “Python”

BUT, apparently it does not work like that.
Output shows simply a blank line.

This should print the last three:


And if I wanted to reverse those then you can use a negative step:


I have not tested that syntax (just going from memory), so I am just making sure you know what could be done.


Thank you for replying.
I tried running this code but it does not work.
Returns same blank line as the code I mentioned for my query.

Any other ideas?
I have just started with ‘Python Mastery’ course by Code with Mosh

This should work:

name = "Python"


When I run this it returns:


Thank you for replying.

I want to print it is reverse.
name =“hello”
Output desired = olleh

Sorry for misunderstanding the question. To reverse the string you can use:

name = "Python"

reverse = name[::-1]

name = "Python"

both ways return

1 Like


Thanks for providing the solution.
Seems like a ‘reverse’ is new function which I’m yet to learn.

Will you kindly explain the 2nd method? This one seems to be related to the simple print() that was taught in the course so far.

It is not a new function, it is a variable that @Manj_P assigned earlier using the step syntax to reverse the string:

That syntax says take the whole string and use a step of -1 to get each character in reverse order. People sometimes define this as a function to help simplify their code:

def reverse(input):
  return input[::-1]

Which can then be used to achieve your desired result AFAICT:


@Manj_P is just doing all of the work inline here rather than extracting the helper variable reverse. There are extra parentheses here which may be confusing to read which is why the helper variable or function version is more readable.

1 Like