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)

你可能感兴趣的:(python,python3,python字符串转数字,字符串转数字,数字字符串,转换)