0

I'm looking for a solution for this, searched and tried several ones, but none working with int datatypes, using python 3.8.5.

begin = 1
end = 273
print ("lines:", begin, "-", end)

result:

lines: 1 - 273

needed:

lines: 1-273

What do I need to do to remove the blank spaces using string and integer variables?

Thx

  • by default print add a space between elements, you can do `print ("lines: ", begin, "-", chunk1_end, sep="")` or create the string to print / use formatting etc. I encourage you to read python doc to save time rather than to ask that kind of question here ... – bruno Mar 22 '21 at 19:14
  • @ChrisCharley *begin* and *chunk1_end* are int so `begin + "-" + chunk1_end`must be `str(begin) + "-" + str(chunk1_end)` ;-) – bruno Mar 22 '21 at 19:18
  • begin + "-" + chunk1_end errors out with TypeError: unsupported operand type(s) for +: 'int' and 'str' – user3017714 Mar 22 '21 at 19:18
  • print ("lines:", str(begin) + "-" + str(end)) works, thx 4 that! – user3017714 Mar 22 '21 at 19:20

3 Answers3

3

You can use f strings, introduced in python 3.6.

begin = 1
end = 273
print(f"lines: {begin}-{end}")
2

try this line it's better

print(f"lines: {begin}-{chunk1_end}")

also makes your code readable and more controllable

1

Instead of using , you can use +

begin = 1
end = 273
print ("lines:", str(begin)+"-"+str(chunk1_end))

or you can use str.format()

begin = 1
end = 273
print ("lines: {}-{}".format(begin, chunk1_end))
Nick
  • 21
  • 2