2
1

More than 1 year has passed since last update.

【Python】min/max関数は文字列にも使える

Posted at

昨日のプロコン解説を見て初めて知ったので、メモ

文字列にmin/max関数を使うと辞書順最小/最大の文字列を返す

サンプル

print(min("a","b"))
print(max("a","b"))

実行結果

a
b

リストについても同様に辞書順最小/最大の文字列を返す

サンプル

print(min(["a","b","c"]))
print(max(["a","b","c"]))

実行結果

a
c

文字列と数値を引数にするとエラーが発生する

サンプル

print(min("2",1))

エラー文

TypeError: '<' not supported between instances of 'int' and 'str'
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1