如何在Python中将字符串转换为数字?
要将字符串转换为数字,有多种方法。让我们一一看看。
使用 int() 将字符串转换为数字
示例
在此示例中,我们将使用 int() 方法将字符串转换为数字 -
# String to be converted myStr = "200" 1. Display the string and it's type print("String = ",myStr) print("Type= ", type(myStr)) 1. Convert the string to integer using int() and display the type myInt = int(myStr) print("nInteger = ", myInt) print("Type = ", type(myInt)) 登录后复制