Search code examples
pythonstringvariablesconcatenation

Proper way to concatenate mixed type variables WITHOUT using f statement


What would be the proper way to combine something like this?

thisID=12345
myVar= '<!',thisID,'!>'

Expected result: <! 12345 !>

Python is using the commas to convert into a tuple, but I need them to be treated as concatenators instead

EDIT:

My version didnt have f statement, so in case anyone else is needing a similar concat without using f, this worked for me

thisNewID=12345

myVar="<! {thisNewID} !>".format(thisNewID=thisNewID)

Solution

  • Here's a more complete answer that I would have appreciated more than all the downvotes.

    The f statement does not work on every version of python. Newer versions can use

    thisNewID=12345
    myVar= f'<! {thisID} !>'
    

    My version happened to be 3.3 and that did NOT work, but below syntax did.

    myVar="<! {thisNewID} !>".format(thisNewID=thisNewID)
    

    To properly answer this question, an experienced person would first have to ask what version, since this IS version specific syntax.