How to reverse each word in a sentence in Python

2 Answers

0 votes
def reverseWords(s): 
  
    words = s.split(" ") 

    revWords = [word[::-1] for word in words] 
      
    s = " ".join(revWords) 
  
    return s 

s = "python java c++ c#"

print(reverseWords(s)) 


         
      
'''
run:
 
nohtyp avaj ++c #c
      
'''

 



answered Apr 23, 2020 by avibootz
0 votes
def reverseWords(s): 
  
    return ' '.join(word[::-1] for word in s.split(" "))

s = "python java c++ c#"

print(reverseWords(s)) 


         
      
'''
run:
 
nohtyp avaj ++c #c
      
'''

 



answered Apr 23, 2020 by avibootz

Related questions

1 answer 173 views
1 answer 160 views
1 answer 194 views
...