LoginSignup
1
0

More than 5 years have passed since last update.

Python3 > print > %つきの文字列の表示をformat % value形式で書けなかった > 例: 3.1415% > 教えていただいた:print("%.4f%%" % 3.141592)

Last updated at Posted at 2018-01-07
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6

時間のかかる処理のprogressを表示しようとしている。
3.1415%のような表記をしようとした。

test_print_percent_180108.py
import numpy as np

print("%.4f" % 3.141592, "%")

print("%.4f\%" % 3.141592)

1つ目のような特殊な書き方では正常に出力された。
2つ目は下記の後半のようなエラーとなった。

run
$ python3 test_print_percent_180108.py 
3.1416 %
Traceback (most recent call last):
  File "test_print_percent_180108.py", line 5, in <module>
    print("%.4f\%" % 3.141592)
ValueError: incomplete format

https://docs.python.jp/3/library/stdtypes.html#old-string-formatting
あたりをざっと見たが該当部分は不明。

別案としては下記だろうか。

print("%.4f" % 3.141592 + "%")

教えていただいた事項

@shiracamus さんのコメントにて記載方法を教えていただきました。

情報感謝です。

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0