4 examples of 'string reverse function in python' in Python

Every line of 'string reverse function in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
15def reverse_str(string):
16 rev_list=[]
17 new_list=list(string)
18 while new_list:
19 rev_list += new_list[-1]
20 new_list.pop()
21 return ''.join(rev_list)

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

812def test_returns_original_string_if_unreversible(self):
813 self.assertEqual(reverse(''), '')
814 self.assertEqual(reverse('x'), 'x')
815 self.assertEqual(reverse('!!!'), '!!!')
5def test_reverse_string(self):
6 self.assertEqual(reverse_string("I make things beep bop beep bop"),"pob peeb pob peeb sgniht ekam I")
7 self.assertEqual(reverse_string("Write code write code"),"edoc etirw edoc etirW")
8 self.assertEqual(reverse_string("Reverse this Last One"),"enO tsaL siht esreveR")
9 print("\nPassed reverse_string with no errors!")
30def reverse(string):
31 n = len(string)
33 # Create a empty stack
34 stack = createStack()
36 # Push all characters of string to stack
37 for i in range(0,n,1):
38 push(stack,string[i])
40 # Making the string empty since all
41 #characters are saved in stack
42 string=""
44 # Pop all characters of string and
45 # put them back to string
46 for i in range(0,n,1):
47 string+=pop(stack)
49 return string

Related snippets