如何在python中将任何数据类型更改为字符串

时间:2021-12-23 16:32:50

How can I change any data type into a string in Python?

如何在Python中将任何数据类型更改为字符串?

9 个解决方案

#1


78  

myvariable = 4
mystring = str(myvariable)  # '4'

also, alternatively try repr:

此外,或者试试repr:

mystring = repr(myvariable) # '4'

This is called "conversion" in python, and is quite common.

这在python中称为“转换”,非常常见。

#2


36  

str is meant to produce a string representation of the object's data. If you're writing your own class and you want str to work for you, add:

str用于生成对象数据的字符串表示形式。如果您正在编写自己的类,并且希望str为您工作,请添加:

def __str__(self):
    return "Some descriptive string"

print str(myObj) will call myObj.__str__().

print str(myObj)将调用myobject .__str__()。

repr is a similar method, which generally produces information on the class info. For most core library object, repr produces the class name (and sometime some class information) between angle brackets. repr will be used, for example, by just typing your object into your interactions pane, without using print or anything else.

repr是一种类似的方法,它通常生成关于类信息的信息。对于大多数核心库对象,repr在尖括号之间生成类名(有时是类信息)。例如,repr将被用于在交互面板中键入对象,而不使用print或其他任何东西。

You can define the behavior of repr for your own objects just like you can define the behavior of str:

您可以为自己的对象定义repr的行为,就像您可以定义str的行为:

def __repr__(self):
    return "Some descriptive string"

>>> myObj in your interactions pane, or repr(myObj), will result in myObj.__repr__()

>>> myObj在您的交互窗格中或repr(myObj)将导致myobject .__repr__()

#3


11  

I see all answers recommend using str(object). It might fail if your object have more than ascii characters and you will see error like ordinal not in range(128). This was the case for me while I was converting list of string in language other than English

我看到了所有推荐使用str(object)的答案。如果您的对象有超过ascii字符,并且您将看到类似序号的错误不在范围(128)。这是我在用英语以外的语言转换字符串列表时的情况

I resolved it by using unicode(object)

我使用unicode(对象)来解决它

#4


10  

Use the str built-in:

使用str内置:

x = str(something)

Examples:

例子:

>>> str(1)
'1'
>>> str(1.0)
'1.0'
>>> str([])
'[]'
>>> str({})
'{}'

...

From the documentation:

从文档:

Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.

返回一个字符串,其中包含一个对象的漂亮的可打印表示。对于字符串,它返回字符串本身。与repr(object)的不同之处在于,str(object)并不总是试图返回可用于eval()的字符串;它的目标是返回一个可打印的字符串。如果没有给出参数,返回空字符串“。

#5


8  

str(object) will do the trick.

str(object)就可以了。

If you want to alter the way object is stringified, define __str__(self) method for object's class. Such method has to return str or unicode object.

如果您想改变对象被字符串化的方式,那么为对象的类定义__str__(self)方法。这种方法必须返回str或unicode对象。

#6


4  

With str(x). However, every data type can define its own string conversion, so this might not be what you want.

str(x)。但是,每个数据类型都可以定义自己的字符串转换,所以这可能不是您想要的。

#7


1  

Just use str - for example:

使用str -例如:

>>> str([])
'[]'

#8


0  

You can use %s in a way like below

您可以使用以下方式使用%s。

>>> "%s" %([])
'[]'

#9


0  

Use formatting:

使用格式:

"%s" % (x)

Example:

例子:

x = time.ctime(); str = "%s" % (x); print str

Output: Thu Jan 11 20:40:05 2018

产量:2018年1月11日20:40:05

#1


78  

myvariable = 4
mystring = str(myvariable)  # '4'

also, alternatively try repr:

此外,或者试试repr:

mystring = repr(myvariable) # '4'

This is called "conversion" in python, and is quite common.

这在python中称为“转换”,非常常见。

#2


36  

str is meant to produce a string representation of the object's data. If you're writing your own class and you want str to work for you, add:

str用于生成对象数据的字符串表示形式。如果您正在编写自己的类,并且希望str为您工作,请添加:

def __str__(self):
    return "Some descriptive string"

print str(myObj) will call myObj.__str__().

print str(myObj)将调用myobject .__str__()。

repr is a similar method, which generally produces information on the class info. For most core library object, repr produces the class name (and sometime some class information) between angle brackets. repr will be used, for example, by just typing your object into your interactions pane, without using print or anything else.

repr是一种类似的方法,它通常生成关于类信息的信息。对于大多数核心库对象,repr在尖括号之间生成类名(有时是类信息)。例如,repr将被用于在交互面板中键入对象,而不使用print或其他任何东西。

You can define the behavior of repr for your own objects just like you can define the behavior of str:

您可以为自己的对象定义repr的行为,就像您可以定义str的行为:

def __repr__(self):
    return "Some descriptive string"

>>> myObj in your interactions pane, or repr(myObj), will result in myObj.__repr__()

>>> myObj在您的交互窗格中或repr(myObj)将导致myobject .__repr__()

#3


11  

I see all answers recommend using str(object). It might fail if your object have more than ascii characters and you will see error like ordinal not in range(128). This was the case for me while I was converting list of string in language other than English

我看到了所有推荐使用str(object)的答案。如果您的对象有超过ascii字符,并且您将看到类似序号的错误不在范围(128)。这是我在用英语以外的语言转换字符串列表时的情况

I resolved it by using unicode(object)

我使用unicode(对象)来解决它

#4


10  

Use the str built-in:

使用str内置:

x = str(something)

Examples:

例子:

>>> str(1)
'1'
>>> str(1.0)
'1.0'
>>> str([])
'[]'
>>> str({})
'{}'

...

From the documentation:

从文档:

Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.

返回一个字符串,其中包含一个对象的漂亮的可打印表示。对于字符串,它返回字符串本身。与repr(object)的不同之处在于,str(object)并不总是试图返回可用于eval()的字符串;它的目标是返回一个可打印的字符串。如果没有给出参数,返回空字符串“。

#5


8  

str(object) will do the trick.

str(object)就可以了。

If you want to alter the way object is stringified, define __str__(self) method for object's class. Such method has to return str or unicode object.

如果您想改变对象被字符串化的方式,那么为对象的类定义__str__(self)方法。这种方法必须返回str或unicode对象。

#6


4  

With str(x). However, every data type can define its own string conversion, so this might not be what you want.

str(x)。但是,每个数据类型都可以定义自己的字符串转换,所以这可能不是您想要的。

#7


1  

Just use str - for example:

使用str -例如:

>>> str([])
'[]'

#8


0  

You can use %s in a way like below

您可以使用以下方式使用%s。

>>> "%s" %([])
'[]'

#9


0  

Use formatting:

使用格式:

"%s" % (x)

Example:

例子:

x = time.ctime(); str = "%s" % (x); print str

Output: Thu Jan 11 20:40:05 2018

产量:2018年1月11日20:40:05