135-1821-9792

lstr函数python str系列函数

菜鸟求大大们解释Python里str函数和repr函数的区别

GG上有结果,转过来,顺便我自己也看看。

成都创新互联公司主要从事网站建设、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务东宁,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

str()一般是将数值转成字符串。 

repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思。

如list,dict使用str()是无效的,但使用repr可以,这是为了看它们都有哪些值,为了显示之用。

EG:

s = 'Hello,world.'

str(s)

'Hello,world.'

repr(s)

'Hello,world.'

str(0.1)

'0.1'

repr(0.1)

'0.10000000000000001'

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'))"

# reverse quotes are convenient in interactive sessions:

... `x, y, ('spam', 'eggs')`

"(32.5, 40000, ('spam', 'eggs'))"

python str函数怎么用?

是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思。

str():将变量转化为字符串类型

a = 1

b = [1, 2, 3]

str_a = str(a)

print(a)

print(type(a))

str_b = str(b)

print(b)

print(type(b))

The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by

the interpreter (or will force a SyntaxError if there is not equivalent syntax). For objects which don't have a particular representation for human consumption, str() will

return the same value as repr(). Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function. Strings and。

str在python中用法

Python中的str可以表示字符串类,也可以是将变量强制转换为字符串的函数,写作str()。str函数是Python内置函数的一种,可以直接使用,无需调用。 扩展资料

python中srt的全称是SubRip Text,srt文件打开方式srt文件可以使用系统自带的.文本处理器来打开,比如notepad.exe,write.exe,word等文件处理软件。在Python中,str 表示字符串类 ,也可以是将变量强制转换为字符串的函数,写作str()。

python中str函数是什么意思

Python中的str可以表示字符串类,也可以是将变量强制转换为字符串的函数,写作str()。str函数是Python内置函数的一种,可以直接使用,无需调用。

Python由荷兰数学和计算机科学研究学会的Guido van Rossum于1990年代初设计,作为一门叫做ABC语言的替代品。Python提供了高效的高级数据结构,还能简单有效地面向对象编程。

Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言,随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发。

Python解释器易于扩展,可以使用C或C++(或者其他可以通过C调用的语言)扩展新的功能和数据类型。Python也可用于可定制化软件中的扩展程序语言。Python丰富的标准库,提供了适用于各个主要系统平台的源码或机器码。

python中str是什么意思?

str函数是Python的内置函数,它将参数转换成字符串类型,其语法格式为str(object),返回object的字符串形式。

python中的__str__函数作用

__str__方法:总结

在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做“魔法”方法,当使用print输出对象的时候,只要自己定义了__str__(self)方法,那么就会打印从在这个方法中return的数据

例子1:如:

class Car:

def __init__(self, newWheelNum, newColor):

    self.wheelNum = newWheelNum

    self.color = newColor

def __str__(self):

    msg = "嘿。。。我的颜色是" + self.color + "我有" + int(self.wheelNum) + "个轮胎..."

    return msg

def move(self):

    print('车在跑,目标:夏威夷')

BMW = Car(4, "白色")

print(BMW)

例子2:如:

class Cat:

"""定义了一个Cat类"""

#初始化对象

def __init__(self, new_name, new_age):

    self.name = new_name

    self.age = new_age

def __str__(self):

    return "%s的年龄是:%d"%(self.name, self.age)

#方法

def eat(self):

    print("猫在吃鱼....")

def drink(self):

    print("猫正在喝kele.....")

def introduce(self):

    print("%s的年龄是:%d"%(self.name, self.age))

#创建一个对象

tom = Cat("汤姆", 40)

lanmao = Cat("蓝猫", 10)

print(tom)

print(lanmao)

运行结果:

汤姆的年龄是:40

蓝猫的年龄是:10


名称栏目:lstr函数python str系列函数
当前路径:http://kswsj.com/article/dooepoi.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号