Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一个元素元组(int)转换为str_Python_String_Tuples - Fatal编程技术网

Python 将一个元素元组(int)转换为str

Python 将一个元素元组(int)转换为str,python,string,tuples,Python,String,Tuples,假设元组具有一个整数: tup = (19201,) 我正在寻找获得此输出的方法: tup = "(19201)" 到目前为止,我已经尝试过: str_tup = "(" + tup[0] + ")" 但它给了我一个错误: TypeError:无法将“int”对象隐式转换为str 你可以用 str(tup).replace(',', '') 或 必须将int显式转换为str比串联更好的方法是使用Python的格式语法: str_tup = '({})'.format(tup[0]) 将

假设元组具有一个整数:

tup = (19201,)
我正在寻找获得此输出的方法:

tup = "(19201)"
到目前为止,我已经尝试过:

str_tup = "(" + tup[0] + ")"
但它给了我一个错误:

TypeError:无法将“int”对象隐式转换为str

你可以用

str(tup).replace(',', '')


必须将int显式转换为str

比串联更好的方法是使用Python的格式语法:

str_tup = '({})'.format(tup[0])
将用您提供的参数(此处:元组值)替换
{}

更好的是,您可以将其推广到具有更高算术性的元组:

str_tup = '({})'.format(', '.join([str(x) for x in tup]))
这使用Python将所有元组元素转换为字符串,然后使用
join
使用连接字符串将它们连接起来

str_tup = '({})'.format(', '.join([str(x) for x in tup]))