Home > Software design >  Python output string with transition characters and double quotes [duplicate]
Python output string with transition characters and double quotes [duplicate]

Time:09-29

I want to output the following string n times.

" add x0,x0,x0      # placefold  \n\t" \

When I use

placeholder=r'" add x0,x0,x0      # placefold  \n\t" \' 
print(placeholder)

EOL while scanning string literal error occurred. I clearly added r in front of the string, why does this error still occur.

When i use

placeholder=r'" add x0,x0,x0      # placefold  \n\t" \ ' 
print(placeholder)

(I added a space before the'.)

There is no error after running. But I don’t want an extra space at the end.

what should I do? What I want to achieve is:

placeholder=(r'" add x0,x0,x0      # placefold  \n\t" \'  "\n")*repeat_number
print(placeholder)

Thanks!

CodePudding user response:

You can use string concatentation without the operator, I suggest using this code:

placeholder=r'" add x0,x0,x0      # placefold  \n\t" ' '\' 
print(placeholder)

Notice, that "ab" is equal to "a" "b" (this works too in C).

CodePudding user response:

If you want placeholder to end with a backslash, consider escaping it:

placeholder = r'" add x0,x0,x0      # placefold  \n\t" \\'
print(placeholder)

Results in: " add x0,x0,x0 # placefold \n\t" \\. It looks like there are two backslashes, but there is actually only one. The console prints \\ to notify a single backslash in this case. Proof:

>>> placeholder[-1]
'\\'
  • Related