# Task 13: Odd reverse Write a Python program to reverse all words of odd lengths. Sample data: ("The quick brown fox jumps over the lazy dog") ("Python Exercises") Sample output: The quick brown fox jumps revo the yzal dog nohtyP Exercises
时间: 2024-01-11 13:05:52 浏览: 124
在Python中实现字典反转案例
Sure, here's a Python program that reverses all words of odd lengths in a given string:
```python
def reverse_odd_words(string):
words = string.split()
for i in range(len(words)):
if len(words[i]) % 2 != 0:
words[i] = words[i][::-1]
return " ".join(words)
```
Here's how you can use it:
```python
string1 = "The quick brown fox jumps over the lazy dog"
string2 = "Python Exercises"
print(reverse_odd_words(string1))
print(reverse_odd_words(string2))
```
Output:
```
The quick brown fox jumps revo the yzal dog
nohtyP Exercises
```
阅读全文