Python/개념학습

문자열의 정렬

수e 2022. 11. 5. 16:01
a = ['cd', 'ef', 'ab']

a.sort()
print(a)	# ['ab', 'cd', 'ef']

 

문자열 자체를 정렬할 경우

s = "adcb"

s1 = s.sort()			# error
s2 = sorted(s)			# ['a','b','c','d'] 
s3 = ''.join(sorted(s))		# "abcd"

파이썬에서 str type은 sort메소드가 없음. sorted(문자열)의 형태로는 가능(이 때 리턴타입은 list)