-1

I'd like to program an alternate print() function, that automatically turns the inputted string into a formatted one, with correct formatting for variables.

So why doesn't this work?

Test = 'Hello!'

def prints(druck):
    for el in druck.split():
        print(f'{el}')

prints('Hello? Test')

The output is:

Hello?
Test

I want the output to be:

Hello?
Hello!

At the moment I only put one word into prints(). In the end I want to be able to print variables inside of longer strings without having to {} them.

My dream would be to have a print() function that would check if a word is a variable (something like if el == {el}?) and prints it out correctly, no matter the format.

This is my first question here :) Sorry for any inconveniences! And thanks.

11
  • Why did you pass 'Test' instead of Test? Was that a mistake, or do you actually want your prints function to take a 4-character string with characters T, e, s, and t, and somehow get 'Hello!' out of that? Commented Dec 29, 2023 at 11:04
  • Thank you for your question! Maybe I oversimplified the example. In the end I want to write prints('jau Test') and get 'jau Hallo!' out of it. I want the string 'Test' to be turned into the variable Test. I will change the question to make it more comprehensible. Commented Dec 29, 2023 at 11:05
  • 2
    @Emse technically it is possible by reading the locals and globals and checking if the variables are there but why specifically do you want this? Having this instead of the normal print sounds like a disaster waiting to happen. Why for example do f-strings not meet your usecase? Commented Dec 29, 2023 at 11:09
  • 5
    "My dream would be to have a print() function that would check if a word is a variable (something like if el == {el}?) and prints it out correctly, no matter the format." - that kind of autodetection is a bad idea. There's a reason Python doesn't automatically do this already. You end up with all sorts of nasty problems when you want to actually print a word that you happened to also be using as a variable name, which is extremely common. Commented Dec 29, 2023 at 11:09
  • 4
    Protip: don't get obsessed with meta programming. Learn to program the way the textbook teaches it. That's already plenty simple in Python, you don't need any helpers to make it even easier. Yes, programming involves typing, you better get used to that. You reduce the amount of typing by writing efficient, lean code; and by using an IDE that helps you with autocompletions. Of course, to be able to write lean code you'll first be writing a ton of crap code before you get the hang of it. Commented Dec 29, 2023 at 11:53

3 Answers 3

1

You could use globals().

def prints(druck):
    for el in druck.split():
        print(globals().get(el, el))

But you should still probably just use normal f-strings for reasons already mentioned in the comments.

Sorry if I misunderstood the question.

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

1 Comment

@ThierryLathuille Oh but it does! Thank you dimani128 and deceze. As I haven't got 15 reputation yet I cant't upvote anything, I'm sorry. And Thierry, sorry, if I messed up my first question so bad that it wasn't understandable… even got downvoted… Thank you for your time and brains.
-1

You are facing this problem because you didn't use the variable as the argument instead you passed a string.

In simple words, variables are used without quotes.

This will solve the problem prints('Hello? ' + Test)

1 Comment

Hi Richard, thank you for your answer! But actually I wanted to know, why my function isn't working. As I see it, it should take each of the words of the string 'Hello?' and 'Test?' and print it as a formatted string f'{Hello?}' and f'{Test}'. But it didn't print f'{Test}', did it? Because if it did, the output should be "Hello!" instead of "Test".
-3

Hey there – Suprisingly I've come up with a solution myself which meets my needs!

def prints(druck):
for el in druck.split():
    try:
        print(eval(el), end=" ")
    except:
        print(el, end=" ")

Now I can just type in whatever I want and python will try to run each word of it as code before displaying.

Thank you for your time.

2 Comments

Please don't do that. Don't use eval. Just try it = 3; from math import sin; prints('it is a sin to use eval, exit and more will bite you') and see the output: 3 is a <built-in function sin> to use (<built-in function eval>,) <IPython.core.autocall.ZMQExitAutocall object at 0x7fb9d4429850> and more will bite you . And that could be much worse.
Tihi… All I can promise is not to use it while programming missile launch sequences ;) I'll be careful!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.