Python中的str()vs repr()函数

For converting any type of data to string type in python there are multiple methods available in its built-in library. With slight different features, multiple methods can be useful for programmers. Here are two methods to convert the data type to string in python.

为了在python中将任何类型的数据转换为字符串类型,其内置库中提供了多种方法。 由于功能略有不同,多种方法可能对程序员有用。 这是在python中将数据类型转换为字符串的两种方法。

1)Python str()函数 (1) Python str() function)

str() is a built-in function in Python that is used to convert any type of data into the string type. For the use of str(), there is no need to import it in the program and it returns the string representation of the value whatever passes with the str() function. For example, if we pass a float or int value in str() function then it will be a string. For a better understanding of it.

str()是Python中的内置函数,用于将任何类型的数据转换为字符串类型。 对于str()的使用,无需将其导入程序中,并且无论str()函数通过什么值,它都将返回值的字符串表示形式。 例如,如果我们在str()函数中传递float或int值,则它将是字符串。 为了更好地理解它。

Examples:

例子:

>>> a=735  
>>> b=777.97  
>>> s1=str(a)
>>> print(s1)
735

>>> s2=str(b)
>>> print(s2)
777.97

>>> type(a) 
<class 'int'> 

>>> type(s1) 
<class 'str'>

>>> type(s2)
<class 'str'>

2)Python repr()函数 (2) Python repr() function)

repr() function is also a built-in function in Python which is slightly different from the str() function. It also returns the string representation of the value whatever passes with the function repr(). There is a slight difference between the str() and repr() function is that when we pass a string in repr() then it returns the string with the single quotation but str() simply return the actual string with no quotation marks.

repr()函数也是Python中的内置函数,与str()函数略有不同。 无论通过函数repr()传递什么,它还返回值的字符串表示形式。 str()和repr()函数之间有一个细微的区别是,当我们在repr()中传递字符串时,它将返回带单引号的字符串,而str()仅返回不带引号的实际字符串。

Let's see some examples for a better understanding of the repr() function and the difference between the str() function.

让我们看一些示例,以更好地理解repr()函数以及str()函数之间的区别

Example:

例:

>>> b=777.45889
>>> s2=repr(b)  
>>> print(s2)
777.45889

>>> p='Includehelp'  
>>> s1=repr(p)
>>> print(s1)
'Includehelp'  

>>> r=str(p)
>>> print(r)
Includehelp

>>> q=str(b)
>>> print(q)
777.45889

>>> type(s1)
<class 'str'>

>>> type(s2)
<class 'str'>

As from the above examples, we have seen that when we pass a string with repr() function then it returns a string with a single quotation mark but when we pass the same string with the str() function then it simply returns the actual string. This is the main difference between the str() and repr() function in Python. Else both methods do the work of conversion of type of the value to string type.

从上面的示例中,我们已经看到,当我们使用repr()函数传递字符串时,它将返回带单引号的字符串,但是当我们使用str()函数传递相同字符串时,它将仅返回实际的字符串。 这是Python中str()和repr()函数之间的主要区别 。 其他两种方法都可以将值的类型转换为字符串类型。

翻译自: https://www.includehelp.com/python/str-vs-repr-functions.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值