1#linear
2
3def reverse(s):
4 str = ""
5 for i in s:
6 str = i + str
7 return str
8
9#splicing
10'hello world'[::-1]
11
12#reversed & join
13def reverse(string):
14 string = "".join(reversed(string))
15 return string
16
17
18
1# Python string/number reverser
2example_number = 12143
3example_string = "Hello there"
4
5def reverse(thing):
6 thing = str(thing) # convert it to a string, just in case it was a number
7 list_of_chars = [char for char in thing]
8 reversed_list_of_chars = []
9 x = -1
10
11 for char in list_of_chars:
12 reversed_list_of_chars.append(list_of_chars[x])
13 x += -1
14
15 reversed_thing = ''.join(reversed_list_of_chars)
16
17 return reversed_thing
18 # Or alternatively do:
19 print(reversed_thing)
20
21# Run it by doing this
22reverse(example_number)
23reverse(example_string)
1# Python string/number reverser
2# Kinda too much code, so don't use this, use the other examples under this answer.
3example_number = 12143
4example_string = "Hello there"
5
6def reverse(thing):
7 thing = str(thing) # convert it to a string, just in case it was a number
8 list_of_chars = [char for char in thing]
9 reversed_list_of_chars = []
10 x = -1
11
12 for char in list_of_chars:
13 reversed_list_of_chars.append(list_of_chars[x])
14 x += -1
15
16 reversed_thing = ''.join(reversed_list_of_chars)
17
18 return reversed_thing
19 # Or alternatively do:
20 print(reversed_thing)
21
22# Run it by doing this
23reverse(example_number)
24reverse(example_string)