+ 1
Getting rid of spaces in output
Is there a way to get rid of the spaces in the output of this code? BDay_Month=random.randint(1,12) BDay_Day=random.randint(1,31) BDay_Year=random.randint(2000,2010) if BDay_Month==4 or 6 or 9 or 11: if BDay_Day==31: BDay_Day==30 if BDay_Month==2: if BDay_Day>=29: if BDay_Year%4==0: BDay_Day=29 else: BDay_Day==28 print("Born:",BDay_Month,"/",BDay_Day,"/", BDay_Year) The output of this code looks like MM / DD / YYYY, How can I get the output look like MM/DD/YYYY?
6 Antworten
+ 3
The f or F in front of strings tell Python to look at the values inside {} and substitute them with the variables values if exists.
+ 3
As đđ˘đ˘đđ¨ đđĄđđ˛đđĽ had said, both string formatting and/or string concatenation can solve this problem.
However, I think the easiest way to solve this (with the fewest modifications) is to use "sep" argument in "print" function.
For example, just add [sep=""] at the tail of your print statement:
print("Born: ", BDay_Month, "/", BDay_Day, "/" , BDay_Year, sep="")
*Edit:
"sep" is a separator used in print function, and its default value is " "(a space), and that is why you saw those annoying blanks.
+ 2
When you ask Question don't write code in question.There is a + icon where you writes your question just click on it and then click on Insert Code and insert your Code.Then peoples will be able to understand your Code easily.
+ 1
đđ˘đ˘đđ¨ đđĄđđ˛đđĽ
What does the f in the first option do? Removing it causes the contents inside the quotation marks to be directly printed. Is the f like a combination of r/raw input and .format?
+ 1
Got it, thanks guys
+ 1
I am happy to help.