Advertisement
Programmin-in-Python

Reversing Specific Words in a User-given Sentence

Apr 24th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. sentence = input("Enter a Sentence : ")
  2. letter = input("Enter a letter : ")
  3.  
  4. if letter in sentence:
  5.     new_sentence = ""
  6.     words = sentence.split()
  7.  
  8.     for i in words:
  9.         if letter in i:
  10.             new_sentence += i[::-1] + " "
  11.         else:
  12.             new_sentence += i + " "
  13.  
  14.     print("Modified Sentence : ", new_sentence)
  15.  
  16. else:
  17.     raise ValueError(f"The Letter {letter} does not exist in the Sentence")
  18.    
  19. """
  20. Output:-
  21.  
  22. Enter a Sentence : the quick brown fox jumps over the lazy dog
  23. Enter a letter : o
  24. Modified Sentence :  the quick nworb xof jumps revo the lazy god
  25. >>>
  26. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement