Ejemplo 1: cadena inversa en python
'hello world'[::-1]
'dlrow olleh'
Ejemplo 2: cómo invertir el orden de las palabras en Python
## initializing the string
string = "I am a python programmer"
## splitting the string on space
words = string.split()
## reversing the words using reversed() function
words = list(reversed(words))
## joining the words and printing
print(" ".join(words))
Ejemplo 3: invertir cada palabra en una cadena de python
def reverse_word_sentence (sentence):
return ' '.join(word[::-1] for word in sentence.split(" "))
# Input: "Split Reverse Join"
# Output: "tilpS esreveR nioJ"
Ejemplo 4: método de cadena inversa Python
#linear
def reverse(s):
str = ""
for i in s:
str = i + str
return str
#splicing
'hello world'[::-1]
#reversed & join
def reverse(string):
string = "".join(reversed(string))
return string
Ejemplo 5: Python 3 rebanada inversa
stringname[::-1]
Ejemplo 6: invertir una cadena o un número en python
# Python string/number reverser
example_number = 12143
example_string = "Hello there"
def reverse(thing):
thing = str(thing) # convert it to a string, just in case it was a number
list_of_chars = [char for char in thing]
reversed_list_of_chars = []
x = -1
for char in list_of_chars:
reversed_list_of_chars.append(list_of_chars[x])
x += -1
reversed_thing = ''.join(reversed_list_of_chars)
return reversed_thing
# Or alternatively do:
print(reversed_thing)
# Run it by doing this
reverse(example_number)
reverse(example_string)
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)