Python: Convert the letters of a given string into alphabetical order
Python Basic - 1: Exercise-126 with Solution
Write a Python program to convert the letters of a given string (same case-upper/lower) into alphabetical order.
Sample Solution-1:
Python Code:
def test(str1):
return ''.join(sorted(str1))
str1 = "PHP"
print("Original string:",str1)
print("Convert the letters of the said string into alphabetical order:",test(str1))
str1 = "javascript"
print("\nOriginal string:",str1)
print("Convert the letters of the said string into alphabetical order:",test(str1))
str1 = "python"
print("\nOriginal string:",str1)
print("Convert the letters of the said string into alphabetical order:",test(str1))
Sample Output:
Original string: PHP Convert the letters of the said string into alphabetical order: HPP Original string: javascript Convert the letters of the said string into alphabetical order: aacijprstv Original string: python Convert the letters of the said string into alphabetical order: hnopty
Flowchart:

Sample Solution-2:
Python Code:
def test(str1):
result = ""
for i in sorted(str1):
result += i
return result
str1 = "PHP"
print("Original string:",str1)
print("Convert the letters of the said string into alphabetical order:",test(str1))
str1 = "javascript"
print("\nOriginal string:",str1)
print("Convert the letters of the said string into alphabetical order:",test(str1))
str1 = "python"
print("\nOriginal string:",str1)
print("Convert the letters of the said string into alphabetical order:",test(str1))
Sample Output:
Original string: PHP Convert the letters of the said string into alphabetical order: HPP Original string: javascript Convert the letters of the said string into alphabetical order: aacijprstv Original string: python Convert the letters of the said string into alphabetical order: hnopty
Flowchart:

Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to reverse a given string in lower case.
Next: Write a Python program to check whether the average value of the elements of a given array of numbers is a whole number or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join