原文: Python str() Function – How to Convert to a String
Python 的原始数据类型包括浮点数、整数、布尔值和字符串。该编程语言提供了几个函数,你可以用来将这些数据类型中的任何一个转换为其他类型。
在这篇文章中我们要看的其中一个函数是 str()
。它是一个内置函数,你可以用它来将任何非字符串对象转换为一个字符串。
str()
函数是什么
str()
函数接收一个非字符串对象并将其转换为字符串。str()
函数接收的这个对象可以是一个浮点数、整数,甚至是一个布尔值。
除了必须转换为字符串的数据外,str()
函数还需要另外两个参数。下面是它接收的所有参数:
- object:你想转换为字符串的数据。这是一个强制性的参数。如果你不提供它,
str()
将返回一个空字符串作为结果。 - encoding:要转换的数据的编码。它通常是
UTF-8
。默认是UTF-8
。 - errors:指定解码失败时的处理方式。你可以为这个参数使用的值包括
strict
、ignore
、replace
和其他。
str() 函数的基本语法
你必须用逗号分隔 str()
函数中的每个参数,而且 encoding 和 errors 的值都必须是字符串:
str(object_to_convert, encoding='encoding', errors='errors')
如何使用 str() 函数
首先,让我们看看如何使用 str()
函数的所有参数:
my_num = 45
converted_my_num = str(my_num, encoding='utf-8', errors='errors')
print(converted_my_num)
如果你运行这段代码,你会得到这个错误:
converted_my_num = str(my_num, encoding='utf-8', errors='errors')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: decoding to str: need a bytes-like object, int found
出现这个错误是因为你使用了 encoding 参数而没有提供字节对象。在这种情况下,你根本就不需要 encoding
和 errors
。你只需要你想转换的数字:
my_num = 45
converted_my_num = str(my_num)
print(converted_my_num) # 45
如果你坚持使用 encoding
和 errors
参数,那么要转换的对象必须是一个字节对象:
my_num = b'45'
converted_my_num = str(my_num, encoding='utf-8', errors='strict')
print(converted_my_num) # 45
如何用 str() 函数将整数和浮点数转换为字符串
你可以这样用 str()
将整数或浮点数转换为字符串:
my_int = 45
my_float = 34.8
# 将两者转换为字符串
converted_my_int = str(my_int)
converted_my_float = str(my_float)
print(converted_my_int) # output: 45
print(converted_my_float) # output: 34.8
你可以看到返回了数字。你也可以用 type()
函数验证结果的类型是字符串:
my_int = 45
my_float = 34.8
# 将两者转换为字符串
converted_my_int = str(my_int)
converted_my_float = str(my_float)
print("Converted integer is", converted_my_int, "and the type of the result is ", type(converted_my_int)) # Converted integer is 45 and the type of the result is <class 'str'>
print("Converted float is", converted_my_float, "and the type of the result is ", type(converted_my_float)) # Converted float is 34.8 and the type of the result is <class 'str'>
你可以看到转换后的整数和浮点数的类型是一个字符串。
如何用 str() 函数将布尔值转换为字符串
如果你愿意,你也可以将布尔型数据转换为字符串:
my_true_bool = True
my_false_bool = False
converted_my_true_bool = str(my_true_bool)
converted_my_false_bool = str(my_false_bool)
print("Converted Boolean is", converted_my_true_bool, "and the type of the result is ", type(converted_my_true_bool)) # Converted Boolean is True and the type of the result is <class 'str'>
print("Converted Boolean is", converted_my_false_bool, "and the type of the result is ", type(converted_my_false_bool)) # Converted Boolean is False and the type of the result is <class 'str'>
如何使用 str() 函数的 encoding 参数对对象进行编码和解码
encoding
参数对于将字符串编码为字节和将字节解码为字符串非常有用。
例如,要将一个字符串编码为字节,你必须这样使用 encoding
方法:
my_str = "Hello world!"
my_bytes = my_str.encode(encoding='UTF-8', errors='strict')
print(my_bytes) # Output: b'Hello, world!'
print(type(my_bytes)) # Output: <class 'bytes'>
要将一个字节解码为字符串,你应该这样使用 decode()
方法:
my_bytes = b'Hello, world!'
my_str = my_bytes.decode(encoding='UTF-8', errors='strict')
print(my_str) # Output: "Hello, world!"
print(type(my_str)) # Output: <class 'str'>
总结
你已经看到,str()
函数在将非字符串对象和原始数据类型转换为字符串方面发挥了作用。
你可能想知道是否可以用 str()
函数将列表、元组和字典等可迭代数据转换为字符串。好吧,如果你这样做,你不会得到一个错误,你将得到的是可迭代数据的原貌:
my_list = ['ant', 'soldier', 'termite']
converted_my_list = str(my_list)
print(converted_my_list) # ['ant', 'soldier', 'termite']
为了将列表转换为字符串,你必须使用 join()
方法:
my_list = ['ant', 'soldier', 'termite']
converted_my_list =' '.join(my_list)
print(converted_my_list) # ant, soldier, termite
print(type(converted_my_list)) # <class 'str'>
对于字典和元组也是这样的。
谢谢你阅读本文。