Write a Python program to sort a string lexicographically


This program is written in the Python programming language and it sorts the input string in two ways: first by sorting each word in the string, and then by sorting the characters in each word in a case-insensitive manner. It does this in two steps:

  • The first step is to get the input string from the user using the "input" function. The input string is stored in the variable "s".
  • The program then uses the "sorted" function to sort the input string "s". The sorted string is then passed as an argument to another call to the "sorted" function, and the "key" argument is set to "str.upper" to sort the characters in each word in a case-insensitive manner. The result of this second call to "sorted" is then printed to the console using the "print" function.

Source Code

s=input("Enter the String :")
print(sorted(sorted(s), key=str.upper))

Output

Enter the String :Tutor
['o', 'r', 'T', 't', 'u']


Example Programs