Imagen del blog

How to reverse a string in Python with slicing

Invertir una cadena con slicing en Python

In Python, we can reverse a text string very easily using the technique of slicing. This is a compact and elegant way to access subsets of sequences such as lists or strings.

texto = "Python Práctico"
texto_invertido = texto[::-1]
print(texto_invertido)

The result will be:

ocitácirP nohtyP

How does it work?

  • texto[::-1] uses slicing with a step of -1, which indicates that we want to traverse the string backwards.
  • It is not necessary to specify the start or end if we want to reverse the entire string.

This trick is useful when we want to check for palindromes or simply reverse texts quickly.

Similar Posts