This article mainly introduces the python string sorting method. it is a very practical technique based on lambda. if you need it, you can refer to the following example to briefly describe the Python string sorting method, it is a very practical technique in Python programming. Share it with you for your reference. The specific method is as follows:
In general, sorting a string in python is quite troublesome:
1. the string type in python cannot be changed directly. You must first put the string to be sorted in the container, such as list.
2. the sort () function of the list container in python does not return a value.
Therefore, several lines of code are required to sort strings in python.
The specific implementation method is as follows:
>>> S = "string" >>> l = list (s) >>> l. sort () >>> s = "". join (l) >>> s 'ginrst'
Programmers who have just switched from C/C ++ and other languages will often feel very accustomed to it, because in C/C ++, these are all a line of statements that can be done. Therefore, a simple string sorting method is provided here.
The implementation code is as follows:
>>> S = "string" >>> s = "". join (lambda x :( x. sort (), x) [1]) (list (s) >>> s 'ginrst'
It is a little difficult to understand because lambda is used, but it is good to figure it out.
I hope this article will help you with Python programming.