Python: Convert the letters of a given string into alphabetical order
Sort String Alphabetically
Write a Python program to convert the letters of a given string (same case-upper/lower) into alphabetical order.
Sample Solution-1:
Python Code:
# Define a function 'test' that sorts the letters of the input string in alphabetical order.
def test(input_str):
# Using the sorted() function to sort the characters in the input string.
# The ''.join() method is then used to concatenate the sorted characters into a string.
return ''.join(sorted(input_str))
# Test the 'test' function with different input strings.
str1 = "PHP"
print("Original string:", str1)
print("Convert the letters of the said string into alphabetical order:", test(str1))
str2 = "javascript"
print("\nOriginal string:", str2)
print("Convert the letters of the said string into alphabetical order:", test(str2))
str3 = "python"
print("\nOriginal string:", str3)
print("Convert the letters of the said string into alphabetical order:", test(str3))
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
Explanation:
Here is a breakdown of the above Python code:
- Test Function (test function):
- The "test()" function is defined to sort the characters of the input string alphabetically using the sorted() function.
- The sorted characters are then concatenated into a string using the "join()" method.
Flowchart:
Sample Solution-2:
Python Code:
# Define a function 'test' that sorts the letters of the input string in alphabetical order.
def test(input_str):
# Initialize an empty string 'result' to store the sorted characters.
result = ""
# Iterate through the sorted characters of the input string.
for char in sorted(input_str):
# Concatenate each sorted character to the 'result' string.
result += char
# Return the final result string with letters in alphabetical order.
return result
# Test the 'test' function with different input strings.
str1 = "PHP"
print("Original string:", str1)
print("Convert the letters of the said string into alphabetical order:", test(str1))
str2 = "javascript"
print("\nOriginal string:", str2)
print("Convert the letters of the said string into alphabetical order:", test(str2))
str3 = "python"
print("\nOriginal string:", str3)
print("Convert the letters of the said string into alphabetical order:", test(str3))
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
Explanation:
Here is a breakdown of the above Python code:
- Test Function (test function):
- The "test()" function is defined to sort the characters of the input string alphabetically using the "sorted()" function.
- An empty string 'result' is initialized to store the sorted characters.
- o A loop iterates through the sorted characters, and each character is concatenated to the 'result' string.
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 and Language Statistics
- Weekly Trends and Language Statistics