zoukankan      html  css  js  c++  java
  • python的int方法实现数据类型转换

    int方法默认以十进制来实现数据类型的转换:

    举例:

    1   str1="123"   #给定的内容最好是纯数字,当然也可以是数字再掺杂点别的,最好别掺杂,因为会报错
    2 
    3   print(type(str1),str)
    4 
    5   v=int(str1)
    6 
    7   print(type(v),v)

    返回的结果为:

    <class 'str'> 123

    <class 'int'> 123

    int方法还可以按照指定的进制来转换成十进制所对应的数值

    举例:

     

    str1="a"
    print(type(str1),str1)
    v=int(str1,base=16)  #将str1以16进制对待,转换为十进制的数值
    print(type(v),v)
    

    返回结果为:

    <class 'str'> a
    <class 'int'> 10

    举例:

    str1="10"
    print(type(str1),str1)
    v=int(str1,base=2)   #将str1以2进制的10来转换到十进制所对应的数值
    print(type(v),v)

    返回结果为:

    <class 'str'> 10
    <class 'int'> 2

  • 相关阅读:
    FormData的使用
    数据绑定
    DOM的映射机制
    leetcode750
    leetcode135
    leetcode41
    leetcode269
    leetcode253
    leetcode42
    leetcode48
  • 原文地址:https://www.cnblogs.com/Kumanon/p/10364566.html
Copyright © 2011-2022 走看看