要將字符串轉換為數字,可以使用以下方法:
num_str = "123"
num = int(num_str)
print(num)
num_str = "3.14"
num = float(num_str)
print(num)
num_str = "abc123"
if num_str.isdigit():
num = int(num_str)
print(num)
else:
print("The string contains non-numeric characters.")