Python Forum
Removing Space between variable and string in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing Space between variable and string in Python
#1
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
Reply
#2
Use an f-string instead of string concatenation: https://docs.python.org/3/tutorial/input...g-literals.
coder_sw99 likes this post
Reply
#3
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
coder_sw99 likes this post
Reply
#4
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']))
coder_sw99 and Gribouillis like this post
Reply
#5
(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.
Reply
#6
Thanks a lot for the help you all!
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,627 Sep-21-2023, 09:17 AM
Last Post: ineuw
  Replacing String Variable with a new String Name kevv11 2 786 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Need help on how to include single quotes on data of variable string hani_hms 5 2,027 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,816 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 961 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Remove a space between a string and variable in print sie 5 1,782 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,299 Jul-11-2022, 08:52 AM
Last Post: japo85
  Simple Python API, need packaging/removing hardcoding zxcv101 2 1,135 Jun-13-2022, 03:09 PM
Last Post: zxcv101
  Can you print a string variable to printer hammer 2 1,949 Apr-30-2022, 11:48 PM
Last Post: hammer
Question How to convert string to variable? chatguy 5 2,398 Apr-12-2022, 08:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020