[英]Making the printf function of C into Python
我想做一個“你好,(名字)”。 通過嘗試使 C printf
函數在 Python 中起作用。
我想把它從這個:
name = input("What's your name? ")
print(f"Hello, {name}.")
進入這個:
name = input("What's your name? ")
printf("Hello, %s.", name)
我開始這樣的功能,我知道它不會工作,因為第二個參數:
def printf(text, ...):
print(text)
如何制作這樣一個正常工作的 printf 函數?
def printf(text, *args):
print(text % args)
這應該可以解決問題,但也許就像在第一個示例中一樣使用 f 字符串,無需“重新發明輪子”,對嗎?
Python 中沒有 printf 函數,但是有 f 字符串。 你在你的一個例子中使用了它,它真的超級簡單和高效
我想你正在期待這樣的事情:
name = input("What's your name?")
print("Hello, %s!" % name)
您可以在此處了解更多信息: learnpython.org 中的字符串格式
您根本不必重寫函數。 你看:
name = input("What's your name? ")
print("Hello,%s."%name) # Basically the same as the printf function.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.