Reputation: 636
I'm printing this in Python (3.6)
print(' 'f' ')
and it shows nothing! What is happening?
It sounds like whatever I put inside the inner ' ', vanish.
Upvotes: 0
Views: 49
Reputation: 280310
' '
is a string literal representing a single space. This is something pretty much any Python programmer needs to be thoroughly familiar with.
f' '
is an f-string. If there were any braces in there, it would do string interpolation, but there are no braces. Like ' '
, it ends up evaluating to a string representing a single space.
When two string literals appear side by side, Python implicitly concatenates them, as if you had used +
, but with super-high precedence. This is an obscure feature that causes more bugs than it's worth.
Putting all that together, ' 'f' '
evaluates to a string containing two spaces, so you don't see anything when you print it, because you can't see spaces.
Upvotes: 4
Reputation: 21275
You are actually defining two strings there.
' '
- this is a normal spacef' '
- this is an f-string or format string. Any string that's prefixed with f
is an f-string. This one also happens to be just a space. So you have two spaces. Side-by-side. Python implicitly concatenates the two strings to give you the result. So the result of print is also just 2 spaces.
If you wanted to print "f", you can do:
print(' \'f\' ')
You need to escape the single quotes.
Upvotes: 1