reverse | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

reverse

how do you reverse a string?

14th Jun 2019, 10:37 AM
Jess Allen
Jess Allen - avatar
4 Answers
+ 4
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.
14th Jun 2019, 10:46 AM
Rincewind
Rincewind - avatar
+ 4
I've written up some examples here: https://code.sololearn.com/cikUaykgQjaA 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... ...so 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.
14th Jun 2019, 11:05 AM
Rincewind
Rincewind - avatar
+ 2
So the variables dont need to be put in square brackets but the one that your indexing does. Thank you
14th Jun 2019, 11:07 AM
Jess Allen
Jess Allen - avatar
+ 1
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?
14th Jun 2019, 10:48 AM
Jess Allen
Jess Allen - avatar