python 怎么把字符串类型转换为数字

如题所述

#coding:utf-8
def translate(st="你要转换的字符串"):
    try:
        num=int(st)
        return num
    except:
        #print "\"%s\" is not an integer."%st
        pass
    try:
        num=float(st)
        return num
    except:
        #print "\"%s\" is not a float."%st
        pass
    try:
        num=complex(st)
        return num
    except:
        #print "\"%s\" is not a float."%st
        pass
    return st #返回原串表示转换失败

温馨提示:内容为网友见解,仅供参考
第1个回答  2017-01-21
int()
相似回答