1

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?

3
  • Why not to use the easy one print("Hello,", name) Commented Aug 28, 2020 at 5:11
  • Well, I wanna try experimenting the functions from a language to another so that I don't have to interpolate languages altogether but rather make certain functions that I could use later on for a simple language-collaborative program. @Imran Commented Aug 30, 2020 at 4:30
  • Seems like a cool project to create a printf function, even if it's not needed, it'd be nice to see how different folks would write it. And at times, it could be quite handy. Commented Feb 4, 2024 at 22:24

4 Answers 4

2
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?

Sign up to request clarification or add additional context in comments.

1 Comment

Well, yes, but actually no. I wanna try to make functions, just like what my name says.
0

there is no printf function in Python, there however is the f string. You used it in one of your examples just go with that its honestly super easy and efficient

1 Comment

I know there isn't, I just wanna make it.
0

I think you are expecting something like this:

name = input("What's your name?")
print("Hello, %s!" % name)

You can learn more here: String Formatting in learnpython.org

1 Comment

Nah, not really.
0

You don't have to rewrite a function at all. You see:

name = input("What's your name? ")
print("Hello,%s."%name) # Basically the same as the printf function.

1 Comment

I want to just for fun, but thanks for showing me this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.