Python Forum

Full Version: Removing Space between variable and string in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Forum,

I don´t know how to remove space between variables and strings.

    print(temp_alt,"°C entsprechen",tem_neu+"°F")
between temp_alt and °C there should be no space. If I insert a + then an error occurs: TypeError: unsupported operand type(s) for +: 'int' and 'str'

Thanks!
SW
Use an f-string instead of string concatenation: https://docs.python.org/3/tutorial/input...g-literals.
As mention use f-string
>>> temp_alt = 99
>>> temp_neu= 105
>>> print(f"{temp_alt}°C entsprechen {temp_neu}°F")
99°C entsprechen 105°F
As with most things Python there are many ways to solve this problem. In addition to using format() or f'strings.
print('How', 'to', 'remove', 'spaces')
print('Set', 'the', 'separator', sep='')
print('Concatinate'+'the'+'strings')
print(''.join(['Join', 'the', 'strings']))
(Aug-03-2021, 12:06 PM)ndc85430 Wrote: [ -> ]Use an f-string instead of string concatenation: https://docs.python.org/3/tutorial/input...g-literals.

Thank you so much for this information.
Thanks a lot for the help you all!
(Aug-03-2021, 03:32 PM)majidkhan6722 Wrote: [ -> ]
(Aug-03-2021, 12:06 PM)ndc85430 Wrote: [ -> ]Use an f-string instead of string concatenation: https://docs.python.org/3/tutorial/input...g-literals.

Thank you so much for this information.

Thanks a lot for the tutorial, its help me lot