簡體   English   中英

如何在字符串旁邊打印變量的值,該變量的值是 integer?

[英]How to print the value of a variable next to a string, which the value of variable is an integer?

你好 Python 編碼員你今天好嗎? 我有個問題...

如何在字符串旁邊打印變量的值,該變量的值是 integer?

我知道一種在 Python 3 中打印變量的方法:

print(variable)

我見過的另一種方式是:

print("Some string here: ", variable)

我在我的代碼上嘗試了它並且它以某種方式工作,但不是我想要的......我的意思是我想要打印出來的代碼行是:

print("Dogs Years: ", human_years)

而這一行的 output 是:

('Dogs Years: ', 19.5)

但預期的 output 是:

Dogs Years: 19.5

如何實現?

編輯:我終於解決了這個問題。 主要問題是我包含了雙引號而不是單引號。

它應該是這樣的: print(f'Dogs Years: {human_years}')其中human_years實際上是變量。

嘗試使用f' 前綴

print(f'Dogs Years: {human_years}')

print('Dog years: {}'.format(human_years))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM