python中str函数(Python里str函数和repr函数有什么区别)
本文目录
- Python里str函数和repr函数有什么区别
- 菜鸟求大大们解释Python里str函数和repr函数的区别
- python str函数怎么用
- python中的str函数
- python str和repr的区别
- python中“str”是什么意思
Python里str函数和repr函数有什么区别
这个简单str是显示给用户用的repr是给机器用的。classA(object):def__str__(self):print“thisisAclass“def__repr__(self):print“thisisreprfunc“a=A()比如printa调用的是a的__str__方法而如果你在python解释器里直接敲a后回车,调用的是a.__repr__()方法
菜鸟求大大们解释Python里str函数和repr函数的区别
GG上有结果,转过来,顺便我自己也看看。
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 sThe 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 =
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。
python中的str函数
上面那个是做合并加法+时的转换,返回的数据如果是整数序列,序列的加法就成了新增整型,并引起错误。而需要的是字符串的合并。下面由于print打印函数默认会对最终输入的对象进行repr()字符表示化处理,所以不需要再次转换字符串
python str和repr的区别
str与repr区别:
1、python中str函数通常把对象转换成字符串,即生成对象的可读性好的字符串,一般在输出文本时使用,或者用于合成字符串。str的输出对用户比较友好适合print输出。
2、pyton中repr函数将一个对象转成类似源代码的字符串,只用于显示。repr的输出对python友好,适合eval函数得到原来的对象。
3、在类中实现__str__和__repr__方法,就可以得到不同的返回,示例代码:
》》》 class test(object): def __repr__(self): return “return test repr() string.“ def __str__(self): return “return test str() string.“》》》 print(str(test()))return test str() string.》》》 print(repr(test()))return test repr() string.python中“str”是什么意思
你这里str.format(**locats())中locats应该是locals吧?locals返回当前作用域的所有局部变量的变量名:变量值组成的字典。例如:当前作用域有两个局部变量x=1,y=’something’则locals()返回字典{’x’:1,’y’:’something’}**locals()在format函数调用里的意思是将locals()返回的字典解包传递给format函数。如果locals返回的如上面的例子里说的一样的话,解包就是将{’x’:1,’y’:’something’}变成x=1,y=’something’于是str.format(**locats())等价于str.format(x=1,y=’something’)format是字符串对象的方法,format的使用可参考python手册。还有什么不懂可以再追问。谢谢。
更多文章:

vulkan和opengl哪个好(Vulkan相比于OpenGL、DX12、Metal和Mantle有什么优势)
2025年4月8日 08:30

space engine(有没有类似Space Engine的游戏推荐可以学习的游戏)
2025年4月3日 23:50

messageboxicon(c#里的MessageBoxlcon控件怎么实现不同的图标)
2025年3月17日 06:10

timeout中文翻译(gateway timeout是什么意思)
2025年2月28日 11:40

whether or not(whether or not怎么用)
2025年3月20日 15:40

程序员好学吗(27岁了一事无成,朋友是程序员一个月大几万的工资,这么大年纪还能学会吗)
2025年3月2日 14:20

电脑502badgateway修复(电脑出现502 Bad Gateway要怎么修复)
2025年3月4日 14:50

eclipse官方下载步骤(怎样下载Eclipse并升级到最新版本)
2025年2月26日 07:10

operate是什么意思(operating是什么意思中文)
2025年2月16日 10:50

signatures(英语Plugin signatures怎么翻译)
2025年4月6日 06:20

gsonformat安装(android studio 怎样安装插件)
2025年3月10日 15:00

provision apk(摩托罗拉xt800可以删除的系统程序有哪些)
2025年3月29日 06:50

handler怎么读(handlerinterceptor怎么读)
2025年4月6日 11:30