Question:
I want to have no space between text and variable’s when I print them specifically so I can have a “$” next to the variable when printed
Repl link:
code snippet print("Your total is $", Total_Cost)
#I want it so when its printed there is no space between the string and the variable
Hey @RyanHill28 !
You have a couple options (there are probably more!):
Concatenating with a +
Concatenating with an fstring
(recommended)
When concatenating with a +
, you need to make sure both are the same types:
print("Your total is $" + str(Total_Cost))
When concatenating with an fstring
, the data types don’t matter, but you need to use curly braces:
print(f"Your total is ${Total_Cost}") # Notice the "f" in front of the string
I hope this helped!
4 Likes
Inside Python print statements you can also specify an optional separator:
print("Your total is $", Total_Cost, sep="")
The default separator is a space, so this is causing the extra space.
Another optional parameter is end
with a newline character "\n"
as default.
4 Likes
system
Closed
February 8, 2023, 7:52pm
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.