reverse | Sololearn: Learn to code for FREE!



how do you reverse a string?

6/14/2019 10:37:45 AM

Jess Allen

4 Answers

New Answer


It depends what language you use; in python for example you can use string slicing: str1 = "Hello World" print(str1[::-1]) result: dlroW olleH String slices are set using the syntax variableName[startPoint : endPoint : step] str1[::-1] means that you don't set a start or end point so you include the whole string; by setting the step as -1 you move backwards through the word by one character at a time.


I've written up some examples here: It's really flexible because you don't have to include all 3! The square brackets work the same as selecting an index, for example: str = "Hello" str[0] is H str[1] is e... by putting the start:end:step in the square brackets you're explaining that's how you want to move through the indices of the string.


So the variables dont need to be put in square brackets but the one that your indexing does. Thank you


so you can set a start and end point by putting them after the colon? Do the variables that you set have to be put in square parentheses? Or can they just be by themselves?