﻿ Python: Convert the letters of a given string into alphabetical order - 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: 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.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿