+ 1
How can I get rid of the space?
hi guys, I most probably have a very simple question but I cannot figure it out. Below line gives you the following output: print('It is the year of', e,'.') Output: It is the year of 5000 . How can I get rid of the automatic space between year and the "dot" at the end of the sentence?
9 Respostas
+ 5
Super Trooper, alright, I'm glad it worked out for you đ
Well, the number within the curly brackets tells you which argument from the format that is going to be used to replace; the {0} will be replaced with the first argument, the {1} will be replaced with the second argument and so on ...
See this example, maybe it explains better than I could : )
name = "Ipang"
hobby = "learn coding"
year = 2017
print("My name is {0} and I {1} here since {2}.".format(name, hobby, year))
first argument (name) replaces {0}
second argument (hobby) replaces {1}
third argument (year) replaces {2}
Hope that clears up a bit ...
+ 3
Use string format function, as follows:
y = 2018
print("Its year {0}.".format(y))
Hth, cmiiw
+ 3
Hi,
You can do like this:
e = 5000
print(f'It is the year of {e}.')
Output: It is the year of 5000.
f before String is important.
+ 3
You can add the separator parameter like this:
print('It is the year of ', e, '.', sep='')
+ 1
Thank you Anna and Slava. I built both in for different arguments. Both works but above my level at the moment for sure. :)
Ipang, Thank you for your reply also but I cannot seem to make yours work.
print("Its year {0}.".format(y))
+ 1
Super Trooper IMO Anna's answer was correct, and it was also the nearest one doing it as you asked, using comma as value separator in print function. Anyways, did you get any error message testing my example? I would help if I could : )
+ 1
with above suggestions I could figure out how to get rid of the extra space at the end of sentence when you break a line with \n
these two give you same output:
print('Thank you, ', initial,' ', name,'!\nWelcome on board!', sep='')
print(f'Thank you, {initial}, {name}!\n''Welcome on board!')
+ 1
Ipang - actually your one works too (I was not sure what {0} means within a string between " "
it gives same result like the rest: Thank you.
print('It is the year of {0}.'.format(e))
print('It is the year of ', e, '.', sep='')
print(f'It is the year of {e}.')
+ 1
It very clear! thank you Ipang