w3resource

Python: Reverse only the vowels of a given string

Python Basic - 1: Exercise-71 with Solution

Reverse Vowels in String

Write a Python program to reverse only the vowels of a given string.

Sample Solution:

Python Code:

# Function to reverse the order of vowels in a string
def reverse_vowels(str1):
    vowels = ""  # Variable to store vowels in their original order
    for char in str1:
        if char in "aeiouAEIOU":  # Check if the character is a vowel
            vowels += char  # Append the vowel to the vowels variable
    result_string = ""  # Variable to store the result string with reversed vowels
    for char in str1:
        if char in "aeiouAEIOU":  # Check if the character is a vowel
            result_string += vowels[-1]  # Append the last vowel from the vowels variable
            vowels = vowels[:-1]  # Remove the last vowel from the vowels variable
        else:
            result_string += char  # Append non-vowel characters as they are
    return result_string

# Test cases
print(reverse_vowels("w3resource"))   
print(reverse_vowels("Python"))
print(reverse_vowels("Perl"))  
print(reverse_vowels("USA"))  

Sample Output:

w3resuorce
Python
Perl
ASU

Explanation:

Here is a breakdown of the above Python code:

  • Define a function named "reverse_vowels()" that takes a string (str1) as input.
  • Initialize an empty string (vowels) to store vowels in their original order.
  • Iterate through each character in the input string. If the character is a vowel, append it to the 'vowels' variable.
  • Initialize an empty string (result_string) to store the result string with reversed vowels.
  • Iterate through each character in the input string. If the character is a vowel, append the last vowel from the vowels variable to the result string and remove it from the 'vowels' variable. If the character is not a vowel, append it to the result string.
  • Return the final result string.
  • Test the function with various input strings and print the results.

Visual Presentation:

Python: Reverse only the vowels of a given string
Python: Reverse only the vowels of a given string
Python: Reverse only the vowels of a given string

Flowchart:

Flowchart: Python - Reverse only the vowels of a given string

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the longest common prefix string amongst an given array of strings. Return false If there is no common prefix.
Next: Write a Python program to check whether a given integer is a palindrome or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-71.php