python3字符串与数字转换

数字转字符串

str(111)

字符串转数字

def transToNum(s):
    """ 字符串转数字 """
    if s.isnumeric():
        return int(s)
    # 处理负数
    elif len(s) > 1 and s[1:].isnumeric() and s[0] == '-':
        return int(s)
    # 处理小数
    else:
        return float(s)

猜你喜欢

转载自blog.csdn.net/BDawn/article/details/109693927