1

I have a raw string like this,

MasterFile_Name = r'C:\Users\ABC\X12345\DEF\File - Test.xlsx'

I want to pass the value of X12345 through a variable.To do that I am doing something like this

MyID = X12345

MasterFile_Name = r'C:\Users\ABC\' + MyID + '\DEF\File - Test.xlsx'

and

MasterFile_Name = r'C:\Users\ABC\' + MyID + r'\DEF\File - Test.xlsx'

They both are not working for me.

Kindly help me with this.

1 Answers1

10

If the intention is to just concatenate it.

using str.format():

MyID = 'X12345'    
MasterFile_Name = r'C:\Users\ABC\{}\DEF\File - Test.xlsx'.format(MyID)    
print(MasterFile_Name)
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • 1
    I have a `LaTeX` raw string `text = r'$QFT=\frac{1}{\sqrt{} \begin{bmatrix}'.format('128')`. The snippet throws error `IndexError: Replacement index 1 out of range for positional args tuple`. – Rajesh Swarnkar Nov 07 '22 at 13:05
  • @RajeshSwarnkar: You should use rf-format string, like ``x = 128`` and ``text = rf"$QFT=\frac{{1}}{{\sqrt{x}}} \begin{{bmatrix}}"``. – ikreb Aug 07 '23 at 13:26