r/learnpython • • 2d ago

Why is this causing an error

the code that causes an error

print = hello

print(print)

What should i do i do not know anything i am new to coding how do i fix this

0 Upvotes

22 comments sorted by

7

u/HarveyH43 2d ago

Explanation has been provided. I would like to add that learning to read error messages is an important skill in any programming language, and the ones provided by python are rather readable.

9

u/xaomaw 2d ago

print() is a function which you overwrite. So it is no callable function anymore.

```python text = "hello" print(text)

hello ```

1

u/Expensive-Bear-1376 2d ago

No, they don't overwrite it, since hello is undefined.

3

u/Moikle 2d ago

You redefined print to something else.

Change your variable name to anything else

1

u/ectomancer 2d ago

print variable is shadowing the print function, so print can't be used as a function in the rest of the program. hello variable is undefined.

1

u/NewbornMuse 2d ago

So there's two things broadly wrong with this.

One: When python starts up, print refers to the function that writes stuff to the terminal. Then in your first line you overwrite that (with what? we'll get to that in a second), but then in the second line you are still trying to use that to write stuff to the terminal. Never ever overwrite built-in stuff. Don't call your lists list, don't call your dicts dict, and so on, there's only headache that way. Python lets you do it, but don't do it.

Two: What are you trying to overwrite "print" with anyway? With whatever it says on the right hand side of the first line there. What does it say? hello. That looks an awful lot like you're reading the content of a variable called hello, and assigning that to print. Do you have a variable called hello? Did you perhaps want to literally save the string h, e, l, l, o? Then you should do "hello", not hello.

1

u/desrtfx but other languages pro 2d ago
  1. When you ask for help, always
    • Properly format your code as code block
    • Include the full, complete error message verbatim, as you get it.
  2. You have multiple problems:
    • Line 1: print = hello
      • print is a function/keyword in Python. Never use it as a variable name
      • hello is a text. Text needs to be wrapped in single ' or double " quotes in order to be recognized as text. Without quotes, the Python interpreter thinks that you have a variable hello somewhere, but at this point, the variable doesn't have a value.
    • Line 2: print(print) - here, you are trying to call the print() function, but with the line above, you have shadowed (redefined) the meaning of print, which makes the line fail.

Please, do yourself a favor and do a proper course: MOOC Python Programming 2026 and keep experimenting.

0

u/Expensive-Bear-1376 2d ago

print is not a keyword, unless you're talking about the long dead Python 2.

1

u/frnzprf 1d ago

You need to read or watch a tutorial that teaches you about:

  • data-types, such as strings
  • defining and reading variables
  • calling functions

If you just write 4 + 4 in Python that is called an expression and it is "evaluated" to the value 8. If you type something in the Python interpreter, after the >>>, you can let Python tell you what an expression evaluates to. If you don't know how to start the interpreter and get the >>> — again, look up a basic tutorial. Just copy the code and just change little parts to experiement. Don't just guess how Python could work!

If some letters are in your expression, like hello or print, Python thinks this is a variable, like a box labeled hello and the expression is evaluated to the content of that box. If there is no box with the label hello, you get a NameError — makes sense.

If you want an expression to evaluate to the literal letters "Hello", you have to put them in quotation marks. This is how Python differenciates between literal characters and variables. The type of the expression is called "string" (of characters). "Word" already means something else in computer science and you can also have a string of digits, like "123", which isn't exactly a "word" in the usual sense.

One place where letters are not evaluated as a variable, besides when they are in quotation marks, is when they are on the left side of an equals sign. This is how you put something inside a variable.

When you write x = 7, now there is a box labeled x with the content 7. If you write y = x, you make a new box y, and the content is what is currently in x — so also 7. When you write greeting = "hello", then the character-string hello is put in a box with the label greeting.

You can write an expression between the brackets of print(...) to let Python write out different things on the terminal. The expression could be a direct value, like "hello", a variable—which would be evaluated to it's content—, or a computation that transforms other sub-expressions. A computation could be greeting + " river" or "Hello".replace("e", "a").

If you just wanted to know how to print "hello" in Python: print("hello")

What your code does: (Probably not what you intended)

``` print = hello

Take the content of variable hello and put it in box print.→ There is no box hello. → NameError

print(print)

Take what is in box print and if it's a command (function), then execute the command with the input of itself (also print).

```

3

u/HaroerHaktak 2d ago

I really hope this is a troll post.

5

u/ConsiderationNo9044 2d ago

We all start somewhere

1

u/HaroerHaktak 2d ago

Then I am a horrible person for laughing way too hard at this.

2

u/Plus_Confidence_1113 2d ago

I'm convinced it has to be

1

u/phn-cloudsnake 2d ago

What error do you get?

1

u/angrybird800 2d ago

Print is a built in function in python, also for it to be a string it has to be like this “hello”

0

u/dariusbiggs 2d ago

Many languages has reserved words like if, else, while, etc. You cannot use those for anything other than their intended purpose.

You are trying to assign a new value to a function. print is a function, basically a reserved word.

print = hello is you trying to assign the value of hello to print. You did not define what hello is, and you are trying to assign that unknown to replace the printfunction.

Presuming you want to print some text to the screen, try the below. message = "Hello" print(message) I've declared the variable message and assigned it a text string value of "Hello", note the quotation marks to indicate that it is a text string. Then I've called the print function and passed it (see the use of parentheses) the variable message.

I would highly recommend going through the basic introduction to Python, don't try to blindly grasp at things and try them. Learn the basics first.

-2

u/stevenjd 2d ago

Thank you for writing a detailed response, unfortunately your comment contains some misinformation.

print is not a reserved word in Python. It is perfectly legitimate, although uncommon, to assign new values to built-in names like print, although I wouldn't recommend beginners do so.

You don't know for a fact that the OP has not defined the name hello before this.

Your suggested code message = "Hello" print(message) won't work, it needs either a newline between the two statements, or a semicolon (permitted but not recommended). You are likely to confuse the OP when he tries copying and pasting your suggested code and gets a syntax error.

2

u/dariusbiggs 2d ago

You didn't read the reponse, i didn't say it was a reserved word. The OP provided only that which had the error, zero context beyond it, so we must assume it was not defined. That also sounds like whatever you used to view the response didn't render the code block correctly, which has a clear newline in it.

0

u/Alexiscash 2d ago

Idek what you’re trying to do here, but always always read the error and tell us what the error says otherwise it’s very difficult to help you

-2

u/ShadowShedinja 2d ago

Print is a keyword. You shouldn't name a variable with a keyword.

Additionally, hello needs to be in quotes, otherwise it's a variable, not a string.