“Preencha a função que aceita um parâmetro de string e reverte cada palavra na string. Todos os espaços na string devem ser retidos.” Respostas de código

Preencha a função que aceita um parâmetro de string e reverte cada palavra na string. Todos os espaços na string devem ser retidos.

# Complete the function that accepts a string parameter, and reverses each word in the string.
# All spaces in the string should be retained.

# Examples

# "This is an example!" ==> "sihT si na !elpmaxe"
# "double  spaces"      ==> "elbuod  secaps"


def reverse_words(text):
    return ' '.join([i[-1::-1] for i in text.split(" ")])
Rakshitha K.S

Preencha a função que aceita um parâmetro de string e reverte cada palavra na string. Todos os espaços na string devem ser retidos. Pitão

# Python code to Reverse each word
# of a Sentence individually
  
# Function to Reverse words
def reverseWordSentence(Sentence):
  
    # Splitting the Sentence into list of words.
    words = Sentence.split(" ")
      
    # Reversing each word and creating
    # a new list of words
    # List Comprehension Technique
    newWords = [word[::-1] for word in words]
      
    # Joining the new list of words
    # to for a new Sentence
    newSentence = " ".join(newWords)
  
    return newSentence
  
# Driver's Code 
Sentence = "GeeksforGeeks is good to learn"
# Calling the reverseWordSentence 
# Function to get the newSentence
print(reverseWordSentence(Sentence))
Rakshitha K.S

Respostas semelhantes a “Preencha a função que aceita um parâmetro de string e reverte cada palavra na string. Todos os espaços na string devem ser retidos.”

Perguntas semelhantes a “Preencha a função que aceita um parâmetro de string e reverte cada palavra na string. Todos os espaços na string devem ser retidos.”

Mais respostas relacionadas para “Preencha a função que aceita um parâmetro de string e reverte cada palavra na string. Todos os espaços na string devem ser retidos.” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código