1
act = input("Enter name of the activity")
cellone = int(input("Enter the order number of activity"))
cell1 = "A" + cellone
worksheet.write(cell1, act)
  

I'm trying to concatenate A and cellone but I'm getting some error saying that I can't concatenate string and an integer, is there a way to do this?

SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46
  • You don't actually need to concatenate the string and integer. The XlsxWriter `write()` function take integer values as well as strings so you could just do this: `worksheet.write(cellone -1, 0, act)`. See the XlsxWriter docs: https://xlsxwriter.readthedocs.io/worksheet.html#worksheet-write – jmcnamara Mar 20 '22 at 14:17

4 Answers4

3

You can change int to a string representation using this:

cell1="A"+str(cellone)

Alternatively, f-string formatting will also work:

cell1=f"A{cellone}"
SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46
  • 1
    I agree with this answer. f-string is a preferred way for Python as of now. Not only it's cleaner, it's faster as well. – Thu Ya Kyaw Mar 20 '22 at 07:06
  • @IamThuYa indeed it's faster and easy, but still a "hack". In a professional environment where thousands of lines are reviewed and maintained, it's very important to maintain the logic behind the writing. In the first case the developer shows that he's casting a string in order to concat with 'A', while in the second it's like reformatting the variable. – Commissar Vasili Karlovic Mar 20 '22 at 08:47
  • @CommissarVasiliKarlovic I agree with maintaining the logic but this is not a "hack". For python at least, we care about pythonic ways. Using f-string provide so much more control over plain casting. If the variable is a float, with f-string I can control how many decimal I want to use, etc. – Thu Ya Kyaw Mar 20 '22 at 13:33
1

str(cellone) returns the value of cellone as a string.

call1="a"+str(cellone)
clickbait
  • 2,818
  • 1
  • 25
  • 61
1

Remove the int in line 2 as follows:

cellone=input("Enter the order number of activity")

It takes the input as a string data type anyway

1

Just dont convert the string to int. correct that to

cellone=input("Enter the order number of activity")
balu
  • 1,023
  • 12
  • 18