Anasayfa
/
Teknoloji
/
3. (30 P.) Write a Python Function Called SortStr() That Takes a String as Parameter, and Returns a New String Which Contains

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.5 (247 Oylar)
Hasan
Uzman doğrulaması
Profesyonel · 6 yıl öğretmeni

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```Example usage:```python>>> sortStr("db4ac")'4abcd'>>> sortStr("AvaTar")'ATaary'```