How to build the following the string in R?
The way R displays the string (using print command) is different from the actual value of this string. Even though you see 2 backslashes for your output, the actual string contains a single backslash. This is because when you type the name of your variable it shows the value within the quotes and within quotes it should use 2 backslashes. When you display the value of the sting using message() or other similar function, when it just prints the value without quotes, then you will see a single back slash only:

#create a vector of names (words)
words &lt;- c("word1","word2","word3")

#add prefix and suffix part to each word
words_vec &lt;- paste0("\\'",words,"\\'")
words_vec
"\\'word1\\'" "\\'word2\\'" "\\'word3\\'"

#Combine the vector into the string
line &lt;-paste(words_vec,collapse='')
line
"\\'word1\\'\\'word2\\'\\'word3\\'"

#Check how the string looks:
message(line)
\'word1\'\'word2\'\'word3\'


By the way, the above (vectorized) method is more efficient than the for-loop method you are using to put all names together.