4 examples of 'reverse string in python' in Python

Every line of 'reverse string 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)
Important

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

30def reverse(string):
31 n = len(string)
32
33 # Create a empty stack
34 stack = createStack()
35
36 # Push all characters of string to stack
37 for i in range(0,n,1):
38 push(stack,string[i])
39
40 # Making the string empty since all
41 #characters are saved in stack
42 string=""
43
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)
48
49 return string
1416@staticmethod
1417def reverse_string_(s):
1418 """'abc' --> 'cba'"""
1419 return ''.join(reversed(list(s)))
812def test_returns_original_string_if_unreversible(self):
813 self.assertEqual(reverse(''), '')
814 self.assertEqual(reverse('x'), 'x')
815 self.assertEqual(reverse('!!!'), '!!!')

Related snippets