Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - String

Sort characters in a string alphabetically - Python Program

Example 1 :

text = "python" print("".join(sorted(text)))

Output

 
OUTPUT  :
hnopty
 

Example 2 :

def sort_string_alphabetically(input_string): """ Sorts the characters in a given string alphabetically. Args: input_string: The string to be sorted. Returns: A new string with the characters sorted alphabetically. """ # Convert the string to a list of characters. char_list = list(input_string) # Sort the list of characters alphabetically. char_list.sort() # Join the sorted characters back into a string. sorted_string = "".join(char_list) return sorted_string # Get input from the user my_string = input("Enter a string: ") # Sort the string and print the result sorted_result = sort_string_alphabetically(my_string) print(f"The sorted string is: {sorted_result}")

Output

 
OUTPUT  :
Enter a string: Python is fun
The sorted string is:   Pfhinnostuy