w3resource

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:

Flowchart: Python - Convert the letters of a given string into alphabetical order.

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:

Flowchart: Python - Convert the letters of a given string into alphabetical order.

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.



Follow us on Facebook and Twitter for latest update.