首页 > 解决方案 > 如何在python中将美国货币转换为字符串

问题描述

我想用逗号将元组值转换为python中的字符串。Ex Input: 3,500.60 and output should be 3,500.6(string type)

value=3,500.60
s=str(value)

这返回(3, 500.6)

但我想要3,500.6

可能吗?

标签: pythonstringpython-2.7

解决方案


我问了一个单行表达式,它给出了这样的输出。无论如何,这个解决了我的问题。

value=3,500.60
b=''
for i in str(value):
    if not i in (' ','(',')'):
        b+=i
print(b)

更新

这就是我想要的:方法1:

print(str(map(lambda x:x if x not in ('(',')') else '',[i for i in value])).strip('[]').replace(' ',''))

方法二:

print(str(value).strip('()').replace(' ',''))

他们都给出了结果3,500.6


推荐阅读