Ad

Making The Printf Function Of C Into Python

- 1 answer

I wanna make a "Hello, (name)." function in Python by trying to make the C printf function.

I want to turn it from this:

name = input("What's your name? ")
print(f"Hello, {name}.")

Into this:

name = input("What's your name? ")
printf("Hello, %s.", name)

I started off the function like this and I knew it wasn't going to work because of the second argument:

def printf(text, ...):
    print(text)

How can I make a properly working printf function like this?

Ad

Answer

def printf(text, *args):
    print(text % args)

That should do the trick, but perhaps just use an f string like you did in the first example, no need to "reinvent the wheel" right?

Ad
source: stackoverflow.com
Ad