Soru
3. (30 p.) Write a Python function called sortStr() that takes a string as parameter,and returns a new string which contains characters of the given string sorted in ascending order. Example1: The function call sortSt("db4ac") will return a string that is "4abcd". Example2: The function call sortStr("AvaTar") will return a string that is "ATaary". Hint: You can convert string to a list, sort the created list and then build result string from this list.
Çözüm
3.2
(236 Oylar)
Irfan
Uzman · 3 yıl öğretmeni
Uzman doğrulaması
Cevap
Here is a Python function called `sortStr()` that takes a string as a parameter and returns a new string containing the characters of the given string sorted in ascending order:```pythondef sortStr(s): # Convert the string to a list of characters char_list = list(s) # Sort the list of characters in ascending order char_list.sort() # Build the result string from the sorted list of characters result = ''.join(char_list) return result```Let's test the function with the examples provided:Example 1: The function call `sortStr("db4ac")` will return a string that is "4abcd".Example 2: The function call `sortStr("AvaTar")` will return a string that is "ATaary".You can use this function to sort any string in ascending order by passing it as a parameter.