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 sortStr("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
4.5247 Voting
Hasan
Profesyonel · 6 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 />Example usage:<br /><br />```python<br />>>> sortStr("db4ac")<br />'4abcd'<br />>>> sortStr("AvaTar")<br />'ATaary'<br />```
Derecelendirmek için tıklayın: