python培训之str()函数转换字符串

发布时间:2023-03-23 09:33:12 人气:84 作者:多测师

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

  1、方法说明

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

  2、语法

  class str(object='')

  3、实例

  >>> s = 'Hello, world.'

  >>> str(s)

  'Hello, world.'

  >>> repr(s)

python培训之str()函数转换字符串

  "'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学习指路:请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线