エラーハンドリング
try:
print(int(S))#とりあえず実行。えらーが出ればexept以下実行
except:
print("Bad String")
エラーハンドリング2
class Calculator(object):
def power(self, n, p):
if n < 0 or p < 0:
raise Exception('n and p should be non-negative')#raise で投げるエラーを定義できる、Exceptionはシステム終了以外の全ての組み込み例外の親クラス
return n**p
myCalculator=Calculator()
T=int(input())
for i in range(T):
n,p = map(int, input().split())
try:
ans=myCalculator.power(n,p)
print(ans)
except Exception as e:
print(e)
文字列の反転
pythonの文字列は変更不可(タプルみたいな感じ)なので、反転するメソッドを使う時は、別物が生成されると考える
new_str = org_str[::-1]
print(new_str)