要将字符串转换为数字,可以使用以下方法:
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.")