使用新的格式化语法,Python 可以轻松地打印千位分隔符:
print ("{0:,.2f} Euro".format(myVariable))
Run Code Online (Sandbox Code Playgroud)
所以逗号就解决了这个问题。
如何使用旧语法来完成此操作 - 而不使用语言环境?
print ("%.2f Euro" % myVariable)
Run Code Online (Sandbox Code Playgroud) 在我的场景(python 2.7)中,我有:
str(var)
其中var是一个变量,有时需要数千个分隔符,例如1,500,但正如您所看到的那样,已转换为字符串(用于连接目的).
我希望能够将该变量打印为具有数千个分隔符的字符串.
我已经阅读了为数字添加格式的解决方案,例如:
>>> '{:20,.2}'.format(f)
'18,446,744,073,709,551,616.00'
Run Code Online (Sandbox Code Playgroud)
但这似乎仅适用于数字,而不适用于已转换为字符串的数字.
谢谢.
编辑: 对于更具体的上下文,这是我的实现场景:
print 'the cost = $' + str(var1) + '\n'
print 'the cost = $' + str(var2) + '\n'
Run Code Online (Sandbox Code Playgroud)
我有几个'vars'.
我读过这里(如何防止数字在Python matplotlib图中更改为指数形式)和这里(Matplotlib:在对数图中禁用十的幂)并尝试了他们的解决方案但无济于事。
如何将 y 轴转换为显示普通十进制数字而不是科学记数法?请注意,这是 Python 3.5.2。
这是我的代码:
#Imports:
import matplotlib.pyplot as plt
possible_chars = 94
max_length = 8
pw_possibilities = []
for num_chars in range(1, max_length+1):
pw_possibilities.append(possible_chars**num_chars)
x = range(1, max_length+1)
y = pw_possibilities
#plot
plt.figure()
plt.semilogy(x, y, 'o-')
plt.xlabel("num chars in password")
plt.ylabel("number of password possibilities")
plt.title("password (PW) possibilities verses # chars in PW")
plt.show()
Run Code Online (Sandbox Code Playgroud) 我真的不知道这个问题的"名称",所以它可能是一个不正确的标题,但问题很简单,如果我有一个数字
例如:
number = 23543
second = 68471243
Run Code Online (Sandbox Code Playgroud)
我想这样做print().
23543
68471243
我希望这解释得足够或者添加评论.任何帮助表示赞赏!
假设我有一个pandas数据框,并且想对所有数字(整数和浮点数)添加千位分隔符,那么有什么简便快捷的方法呢?
简单的任务:
number = 100123.44
number_formated = "100,123"
Run Code Online (Sandbox Code Playgroud)
不要告诉我没有比以下更好的方法了:
number_formated = "{:,}".format(int(format("{:.0f}".format(number))))
Run Code Online (Sandbox Code Playgroud)
?
我有一个像这样的 numpy 数组:
[ 1024 303 392 4847 7628 6303 8898 10546 11290
12489 19262 18710 20735 24553 24577 28010 31608 32196
32500 32809 37077 37647 44153 46045 47562 48642 50134
50030 52700 52628 51720 53844 56640 56856 57945 58639
57997 63326 64145 65734 67148 68086 68779 68697 70132
71014 72830 77288 77502 77537 78042 79623 81151 81584
81426 84030 86879 86171 89771 88367 90440 92640 93369
93818 97085 98787 98867 100471 101473 101788 102828 104558
105144 107242 107970 109785 …Run Code Online (Sandbox Code Playgroud) 我使用ipython 5.8.0的Debian 10。
这是输出的样子:
In [1]: 50*50
Out[1]: 2500
Run Code Online (Sandbox Code Playgroud)
是否可以配置ipython为使用千位分隔符打印所有数字?IE:
In [1]: 50*50
Out[1]: 2'500
In [2]: 5000*5000
Out[2]: 25'000'000
Run Code Online (Sandbox Code Playgroud)
也许,是否有可能ipython在输入时也理解千位分隔符?
In [1]: 5'000*5'000
Out[1]: 25'000'000
Run Code Online (Sandbox Code Playgroud)
接受的答案@Chayim Friedman适用于整数,但不适用于浮点数:
In [1]: 500.1*500
Out[1]: 250050.0
Run Code Online (Sandbox Code Playgroud)
此外,当它工作时,它,用作千位分隔符的字符:
In [1]: 500*500
Out[1]: 250,000
Run Code Online (Sandbox Code Playgroud)
我可以用'吗?
我想将数字1323.67转换为1.323,67,我该怎么做?
我已经试过了
f'{1325.76:.,2f}'
但它打印出1,325.76
我期望f'{1325.76:.,2f}'是1.325,75但它是1,325.76
python ×8
formatting ×2
numbers ×2
python-3.x ×2
arrays ×1
decimal ×1
format ×1
ipython ×1
matplotlib ×1
numpy ×1
optimization ×1
pandas ×1
plot ×1
python-2.7 ×1
python-3.7 ×1
scipy ×1
separator ×1
string ×1