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.2236 Voting
Irfan
Uzman · 3 yıl öğretmeniUzman 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:<br /><br />```python<br />def sortStr(s):<br /> # Convert the string to a list of characters<br /> char_list = list(s)<br /> <br /> # Sort the list of characters in ascending order<br /> char_list.sort()<br /> <br /> # Build the result string from the sorted list of characters<br /> result = ''.join(char_list)<br /> <br /> return result<br />```<br /><br />Let's test the function with the examples provided:<br /><br />Example 1: The function call `sortStr("db4ac")` will return a string that is "4abcd".<br /><br />Example 2: The function call `sortStr("AvaTar")` will return a string that is "ATaary".<br /><br />You can use this function to sort any string in ascending order by passing it as a parameter.
Derecelendirmek için tıklayın: