• 技术文章 >Python技术 >Python基础教程

    python中str()函数转换字符串

    小妮浅浅小妮浅浅2021-03-30 16:09:57原创7733

    1、方法说明

    如果只是想把Python的对象转换成文字串的话,str()函数是回到人类可读值的表示。

    2、语法

    class str(object='')

    3、实例

    >>> s = 'Hello, world.'
    >>> str(s)
    'Hello, world.'
    >>> repr(s)
    "'Hello, world.'"
    >>> str(1/7)
    '0.14285714285714285'
    >>> x = 10 * 3.25
    >>> y = 200 * 200
    >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
    >>> print(s)
    The value of x is 32.5, and y is 40000...
    >>> # The repr() of a string adds string quotes and backslashes:
    ... hello = 'hello, world\n'
    >>> hellos = repr(hello)
    >>> print(hellos)
    'hello, world\n'
    >>> # The argument to repr() may be any Python object:
    ... repr((x, y, ('spam', 'eggs')))
    "(32.5, 40000, ('spam', 'eggs'))"

    以上就是python中str()函数转换字符串的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

    专题推荐:python str函数
    品易云
    上一篇:python format的格式化操作 下一篇:python str.zfill填充字符串

    相关文章推荐

    • python常量是什么• python random中的随机函数• python函数式编程如何理解• python loguru如何记录日志• python使用loguru操作日志• python input()的特性• python列表中有哪些内置函数• python系统内置方法如何获取• python列表追加元素出错的解决• python匿名函数lambda的注意点• python列表的数据类型分析

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网