콘텐츠로 건너뛰기

파이썬 문자열 뒤집는 방법

[

Reverse Strings in Python: reversed(), Slicing, and More

Reversing Strings With Core Python Tools

Reversing Strings Through Slicing

  • In Python, you can reverse a string using slicing.
  • Slicing is a technique that allows you to extract items from a sequence using integer indices, known as offsets.
  • To reverse a string using slicing, you can use the slicing operator [::-1].
  • This operator extracts the characters from the string in reverse order.
  • Here’s an example:
string = "ABCDEF"
reversed_string = string[::-1]
print(reversed_string) # Output: FEDCBA

Reversing Strings With .join() and reversed()

  • Python also provides the built-in function reversed() which creates an iterator that yields the characters of a string in reverse order.
  • You can combine reversed() with the .join() method to create a reversed copy of a string.
  • The .join() method concatenates the characters from the reversed iterator into a single string.
  • Here’s an example:
string = "ABCDEF"
reversed_string = ''.join(reversed(string))
print(reversed_string) # Output: FEDCBA

Generating Reversed Strings by Hand

Reversing Strings in a Loop

  • Another method to reverse a string is by using a loop.
  • You can iterate through the characters of the string and build a reversed copy character by character.
  • Here’s an example:
string = "ABCDEF"
reversed_string = ""
for char in string:
reversed_string = char + reversed_string
print(reversed_string) # Output: FEDCBA

Reversing Strings With Recursion

  • Recursion is another technique that can be used to reverse a string.
  • You can define a recursive function that takes a string as input and returns the reversed string.
  • The function calls itself recursively to build the reversed string character by character.
  • Here’s an example:
def reverse_string(string):
if len(string) == 0:
return string
else:
return reverse_string(string[1:]) + string[0]
string = "ABCDEF"
reversed_string = reverse_string(string)
print(reversed_string) # Output: FEDCBA

Using reduce() to Reverse Strings

  • Python’s functools module provides the reduce() function, which can be used to apply a function to a sequence and accumulate the results.
  • By using reduce() with a lambda function, you can reverse a string.
  • Here’s an example:
from functools import reduce
string = "ABCDEF"
reversed_string = reduce(lambda x, y: y + x, string)
print(reversed_string) # Output: FEDCBA

Iterating Through Strings in Reverse

The reversed() Built-in Function

  • Python provides the built-in function reversed(), which can be used to iterate through a string in reverse order.
  • reversed() returns an iterator that yields the characters of the string in reverse order.
  • You can use a for loop to iterate over the reversed iterator.
  • Here’s an example:
string = "ABCDEF"
for char in reversed(string):
print(char)

The Slicing Operator, [::-1]

  • As mentioned earlier, you can use the slicing operator [::-1] to create a reversed copy of a string.
  • You can also use it to iterate through a string in reverse order.
  • Here’s an example:
string = "ABCDEF"
for char in string[::-1]:
print(char)

Creating a Custom Reversible String

  • You can create a custom class that represents a reversible string.
  • This class can have methods to reverse the string and iterate through it in reverse order.
  • Here’s an example:
class ReversibleString:
def __init__(self, string):
self.string = string
def reverse(self):
return self.string[::-1]
def __iter__(self):
return reversed(self.string)

Sorting Python Strings in Reverse Order

  • Python provides the sorted() function, which can be used to sort a sequence of strings.
  • By using the reverse=True parameter, you can sort the strings in reverse order.
  • Here’s an example:
strings = ["ABC", "DEF", "GHI"]
sorted_strings = sorted(strings, reverse=True)
print(sorted_strings) # Output: ['GHI', 'DEF', 'ABC']

Conclusion

  • Reversing strings in Python can be done using various techniques and tools.
  • You can use slicing, the reversed() function, loops, recursion, or even create a custom reversible string class.
  • Additionally, you can iterate through strings in reverse order using the reversed() function or the slicing operator [::-1].
  • Sorting strings in reverse order can be done using the sorted() function with the reverse=True parameter.
  • By mastering these techniques, you can efficiently work with reversed strings in Python.