美文网首页Python
Python 数据类型转换

Python 数据类型转换

作者: 57fc17b7d598 | 来源:发表于2016-03-14 16:33 被阅读40次

1. 整数

  • 正确示范
    <pre>

返回 123

int("123")

返回 123

int(123)

返回 123

int(123.123456)
</pre>

  • 错误示范
    <pre>
    int("a")
    int("123.456")
    </pre>

2. 浮点数

  • 正确示范
    <pre>

返回 123.0

float(123)

返回 123.0

float("123")

返回 123.456

float(123.456)

返回 -123.456

float("-123.456")
</pre>

  • 错误示范
    <pre>
    float("a")
    </pre>

3. 字符串

  • 万能的str
    <pre>
    str(123)
    str("abc")
    str(True)
    </pre>

4. 布尔型

  • 返回 True
    <pre>
    bool(1)
    bool("1")
    bool("0")
    bool("false")
    </pre>
  • 返回 False
    <pre>
    bool()
    bool(0)
    bool("")
    </pre>

5. 集合

  • 将字符串转为集合
    <pre>

返回["s", "t", "r", "i", "n", "g"]

list("string")
</pre>

相关文章

网友评论

    本文标题:Python 数据类型转换

    本文链接:https://www.haomeiwen.com/subject/rapflttx.html