python拼接字符串repr()和``
python拼接字符串
python中如何像php,javascript一样把字符串或数字连在一起呢 ?就是将两个东西拼接起来。
对数字,如果拼接,就是对两个数字求和。如:3+5,就计算出为8。那么对字符串都能进行什么样的操作呢?试试吧:
>>> "py" + "thon" 'python'
跟我那个不为大多数人认可的发现是一样的,你还不认可吗?两个字符串相加,就相当于把两个字符串连接起来。(别的运算就别尝试了,没什么意义,肯定报错,不信就试试)
>>> "py" - "thon" #这么做的人,是脑袋进水泥了吧? Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'str'
用+
号实现连接,的确比较简单,不过,有时候你会遇到这样的问题:
>>> a = 1989 >>> b = "free" >>> print b+a Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects
这里引入了一个指令:
print(b+a)
的样式了。
报错了,其错误原因已经打印出来了(一定要注意看打印出来的信息):cannot concatenate 'str' and 'int' objects
。原来a
对应的对象是一个int
类型的,不能将它和str
对象连接起来。怎么办?
原来,用+
拼接起来的两个对象,必须是同一种类型的。如果两个都是数字,毫无疑问是正确的,就是求和;如果都是字符串,那么就得到一个新的字符串。
修改上面的错误,可以通过以下方法:
>>> print b + `a` free1989
注意,` ` 是反引号,不是单引号,就是键盘中通常在数字1左边的那个,在英文半角状态下输入的符号。这种方法,在编程实践中比较少应用,特别是在python3中,已经把这种方式弃绝了。我想原因就是这个符号太容易和单引号混淆了。在编程中,也不容易看出来,可读性太差。
常言道:“困难只有一个,解决困难的方法不止一种”,既然反引号可读性不好,在编程实践中就尽量不要使用。于是乎就有了下面的方法,这是被广泛采用的。不但简单,更主要是直白,一看就懂什么意思了。
>>> print b + str(a) free1989
用str(a)
实现将整数对象转换为字符串对象。虽然str是一种对象类型,但是它也能够实现对象类型的转换,这就起到了一个函数的作用。其实前面已经讲过的int也有类似的作用。比如:
>>> a = "250" >>> type(a) <type 'str'> >>> b = int(a) >>> b 250 >>> type(b) <type 'int'>
提醒列位,如果你对int和str比较好奇,可以在交互模式中,使用help(int),help(str)查阅相关的更多资料。
还有第三种:
>>> print b + repr(a) #repr(a)与上面的类似 free1989
这里repr()是一个函数,其实就是反引号的替代品,它能够把结果字符串转化为合法的python表达式。
可能看官看到这个,就要问它们三者之间的区别了。首先明确,repr()和``是一致的,就不用区别了。接下来需要区别的就是repr()和str,一个最简单的区别,repr是函数,str是跟int一样,一种对象类型。不过这么说是不能完全解惑的。幸亏有那么好的google让我辈使用,你会找到不少人对这两者进行区分的内容,我推荐这个:
When should i use str() and when should i use repr() ?
Almost always use str when creating output for end users.
repr is mainly useful for debugging and exploring. For example, if you suspect a string has non printing characters in it, or a float has a small rounding error, repr will show you; str may not.
repr can also be useful for for generating literals to paste into your source code. It can also be used for persistence (with ast.literal_eval or eval), but this is rarely a good idea--if you want editable persisted values, something like JSON or YAML is much better, and if you don't plan to edit them, use pickle.
2.In which cases i can use either of them ?
Well, you can use them almost anywhere. You shouldn't generally use them except as described above.
3.What can str() do which repr() can't ?
Give you output fit for end-user consumption--not always (e.g., str(['spam', 'eggs']) isn't likely to be anything you want to put in a GUI), but more often than repr.
4.What can repr() do which str() can't
Give you output that's useful for debugging--again, not always (the default for instances of user-created classes is rarely helpful), but whenever possible.
And sometimes give you output that's a valid Python literal or other expression--but you rarely want to rely on that except for interactive exploration.
以上英文内容来源:http://stackoverflow.com/questions/19331404/str-vs-repr-functions-in-python-2-7-5
扫描二维码推送至手机访问。
版权声明:本文由学无止境-开拓创新-ipvb学习网发布,如需转载请注明出处。