質問
僕がどのようにフォーマットの数字として文字列です。私のコードはこちら:
return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm
時間分の整数、および秒は浮いてしまうからなのです。str()関数に変換しこれらすべての番号を割(0.1)。なので私の文字列を出力す"5:30:59.07pm"で表示すように"5.0:30.0:59.1pm")。
ボトムライン、図書館の機能はかかりますかいしてもらえますか?
解決
Python3.6において、フォーマットPythonでをベースに制御を行っています。 形式の文字列リテラル または f-文字列:
hours, minutes, seconds = 6, 56, 33
f'{hours:02}:{minutes:02}:{seconds:02} {"pm" if hours > 12 else "am"}'
または str.format
機能から2.7:
"{:02}:{:02}:{:02} {}".format(hours, minutes, seconds, "pm" if hours > 12 else "am")
または 文字列のフォーマット %
オペレーター も古いバージョンのPythonでの注を参照してくださいのドキュメント:
"%02d:%02d:%02d" % (hours, minutes, seconds)
は、お客様の特定の場合はフォーマット時間が time.strftime
:
import time
t = (0, 0, 0, hours, minutes, seconds, 0, 0, 0)
time.strftime('%I:%M:%S %p', t)
他のヒント
からPython2.6である代替:の str.format()
方法。この使用例を示既存の文字列形式のオペレーター(%
):
>>> "Name: %s, age: %d" % ('John', 35)
'Name: John, age: 35'
>>> i = 45
>>> 'dec: %d/oct: %#o/hex: %#X' % (i, i, i)
'dec: 45/oct: 055/hex: 0X2D'
>>> "MM/DD/YY = %02d/%02d/%02d" % (12, 7, 41)
'MM/DD/YY = 12/07/41'
>>> 'Total with tax: $%.2f' % (13.00 * 1.0825)
'Total with tax: $14.07'
>>> d = {'web': 'user', 'page': 42}
>>> 'http://xxx.yyy.zzz/%(web)s/%(page)d.html' % d
'http://xxx.yyy.zzz/user/42.html'
ここでは、スニペットを活用した str.format()
:
>>> "Name: {0}, age: {1}".format('John', 35)
'Name: John, age: 35'
>>> i = 45
>>> 'dec: {0}/oct: {0:#o}/hex: {0:#X}'.format(i)
'dec: 45/oct: 0o55/hex: 0X2D'
>>> "MM/DD/YY = {0:02d}/{1:02d}/{2:02d}".format(12, 7, 41)
'MM/DD/YY = 12/07/41'
>>> 'Total with tax: ${0:.2f}'.format(13.00 * 1.0825)
'Total with tax: $14.07'
>>> d = {'web': 'user', 'page': 42}
>>> 'http://xxx.yyy.zzz/{web}/{page}.html'.format(**d)
'http://xxx.yyy.zzz/user/42.html'
のようにPython2.6+、全てのPython3リリース(な)方法をご理解います。I shamelesslyゥこのもの 私のハードコアなPythonの紹介書 スライドは、Intro+中間 Pythonコースを提供し 利用開始日から開始されます。 :-)
日2018更新:もちろん、そして f-文字列の特徴を3.6, し、相当の事例 その, ありもの:
>>> name, age = 'John', 35
>>> f'Name: {name}, age: {age}'
'Name: John, age: 35'
>>> i = 45
>>> f'dec: {i}/oct: {i:#o}/hex: {i:#X}'
'dec: 45/oct: 0o55/hex: 0X2D'
>>> m, d, y = 12, 7, 41
>>> f"MM/DD/YY = {m:02d}/{d:02d}/{y:02d}"
'MM/DD/YY = 12/07/41'
>>> f'Total with tax: ${13.00 * 1.0825:.2f}'
'Total with tax: $14.07'
>>> d = {'web': 'user', 'page': 42}
>>> f"http://xxx.yyy.zzz/{d['web']}/{d['page']}.html"
'http://xxx.yyy.zzz/user/42.html'
Python2.6+
使用可能で、 format()
機能がご利用できる:
return '{:02d}:{:02d}:{:.2f} {}'.format(hours, minutes, seconds, ampm)
複数の使用方法や機能、さらなる情報で確認することができ 文書.
Python3.6+
f-文字列である新たな機能が追加されている言語のPython3.6.このページに関連するメニューのフォーマット文字列の悪名:
return f'{hours:02d}:{minutes:02d}:{seconds:.2f} {ampm}'
利用できるCスタイルの文字列のフォーマット:
"%d:%d:d" % (hours, minutes, seconds)
こちらをご参照は、特に: https://web.archive.org/web/20120415173443/http://diveintopython3.ep.io/strings.html
利用できる以下の達成に機能
"%d:%d:d" % (hours, minutes, seconds)
str() pythonでは、整数ま ない 印小数点ます。
またfloatしたいを無視する小数部を利用することができstr(int(floatValue)).
おそらく以下のコードを発揮します:
>>> str(5)
'5'
>>> int(8.7)
8
とした場合を含む値を小数、小数点以下が無視できる(ie:100.0)努めるintを取得しまエラーとなります。そうでんが、呼び出しfloat初の修正。
str(int(float([変数])))