0
0

大文字小文字の反転 Python3編

Posted at

1文字ずつ、大文字は小文字に、小文字は大文字にして出力せよという問題
pythonにはそれができる関数があるので

print(input().swapcase())

これにて終わりですが、もしその関数がなかったらどう実装するか。
ひとまずその1文字ずつ切り出して大文字か小文字かを調べて変換するというのが考えられる。

1文字ずつ切り出す方法は以前やった。
小文字かを調べるのはislower()
他にもisupper(),istitle()がある。

s = input()

for character in s:
    if character.islower():
        print(character.upper(), end="")
    else:
        print(character.lower(), end="")

print()
0
0
0

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
0
0