(Jan-15-2021, 10:28 AM)buran Wrote: There are multiple issues/misunderstandings, apart from not using BBCode tags right.Hi Buran,
- You are using python interactive shell at the moment (that is having >>> when you write code). This is good for experiments, but normally you would write your code in a file, save that file with
.py
extension and run it. Using interactive shell makes a difference because each line is evaluated the moment you hitEnter
. In other words - at the moment you don't print. I guess it "worked" because you missed theprint=
part and it just evaluated the expression(f'Hello World') + inv
.
- when you do
print=('Hello World')
you are doing an assignment. Note that in(Hello World')
the brackets are redundant. it's the same as if you are doingprint='Hello World'
. Now, name'Hello World'
(it's a poor name, because it overrides the built-in function, but I know you didn't mean to).
>>> print=('Hello World') >>> print 'Hello World' >>>Note, this is NOT printing, it evaluate and display name
- @Axel_Erfurt in his post #2 show you what you have to do. There are other possible approaches. In his first example, he pass f-string (from formatted-string)
f'Hello World {inv}'
as single argument toprint()
function. That is what will be printed. Note that we assume you already have defined nameinv
. If you did not - this will cause error.
>>> inv = 'World' >>> f'Hello {inv}' 'Hello World'Again, this is not printing, it just evaluates thef'Hello {inv}'
and replace{inv}
with value ofinv
and display the result.
In the second he passarguments to print function. As a result, when
print()` function is executed, it prints all arguments, separated by space. You can change that behavior and specify different separator, but that's yet another thing.
- There are multiple ways to construct a string from different parts. The f-strings are the latest addition to so called string-formatting`.
- now, you need first to restart your python interactive shell, if you didn't do so yet, so that you can fix the problem with
print()
function being overriden by your assignments.
>>> inv = 'World' >>> print(f'Hello {inv}') Hello Worldnow, this is printing. Do you see the difference? in the previous example there were single quotes aroundHello World
and here there are none.
- As I said better work in a file, not in the interactive shell, so that you don't lose your work, you can make small changes and run the file again and see what happens.
- In your post you said it worked with
(f'Hello World') + inv
. I already mentioned that brackets are redundant here, but also the f is redundant, because you are not doing any string formatting (no{}
inside the string). So it's the same as'Hello World' + inv
. This is string concatenation and it will work only ifinv
is alsostr
, if it is e.g. numberint
it will not work.
All that said, I strongly advise you to find a tutorial and follow it, so that you get familiar with the very basic concepts. Otherwise it will be hard and time-consuming.
For the tags - look again at the link in my moderator note.
EDIT: OP solved their problem, but I already have written this extensive post, so I will keep it.
thank you for this heads up, i love this community and sorry again for the wrong BB code used