問題描述
如何將坐標(元組)格式化為字符串? (How can I format a coordinate (tuple) as a string?)
我需要將坐標 ex: C3 = (2,3)
轉換為在此示例中返回 "(2 : 3)"
的字符串。
y = str(coordenada_linha) #correspond value 3
x = str(coordenada_coluna) #correspond value 2
return str((x : y))
這段代碼在我看來是完全錯誤的,我不知道要以正確的方式返回。
參考解法
方法 1:
def convert_to_str(c):
return "(%d : %d)"%(c[0],c[1])
>>> C3 = (2,3)
>>> print(convert_to_str(C3))
>>> (2 : 3)
</code></pre>
(by Francisco Coito、Jeff Mandell)
參考文件