0

I have a list of strings which looks like this: B = [' _ ', '|_\', '|_/']

(if you put these strings on top of each other you get the character B).

So I have this list of strings but Visual Studio Code says that this string literal is not terminated...

I assume it means the second element in the list as it has a backslash. What can I do about it? I tried double backslash, but then it just prints double backslash if I print the line

Thanks in advance

12
  • @Thomas yes but one of the ' is escaped Commented Feb 13, 2021 at 14:09
  • this seems fine to me Commented Feb 13, 2021 at 14:10
  • Works fine if I copy it verbatim to the Python shell: >>> B = [' _ ', '|_\'', '|_/']. Might there be any hidden characters or "smart" quotes that you didn't show here? Commented Feb 13, 2021 at 14:11
  • when i place the code in a python repl, it works just fine, either something is wrong with you reading the error (maybe u didnt save or something) or this is a major bug in vscode Commented Feb 13, 2021 at 14:11
  • Here are my examples: A = [' _ ', '|_|', '| |'] B = [' _ ', '|_\'', '|_/'] C = [' _ ', '| ', '|_ '] D = [' _ ', '| \'', '|_/'] E = [' _ ', '|_ ', '|_ '] F = [' _ ', '|_ ', '| '] and I just call print(B) Commented Feb 13, 2021 at 14:12

2 Answers 2

3

So i assume your code looks something like this:

def func(str):
    print("\n".join(str))

A = [' _ ', '|_|', '| |']
B = [' _ ', '|_\'', '|_/']
C = [' _ ', '|  ', '|_ ']
D = [' _ ', '| \'', '|_/']
E = [' _ ', '|_ ', '|_ ']
F = [' _ ', '|_ ', '|  ']

func(A)
func(B)
func(C)
func(D)
func(E)
func(F)


This gives us this output:

 _ 
|_'
|_/

I assume the output you are looking for is more like this:

_ 
|_\
|_/

You want that ' to be gone and replaced with , this doesnt mean the string is not terminated, it means that you escaped a quote. Lets take a look at the string that has this error, its the second string in the B array:

'|_''

first it prints a | then it prints a _ and then it prints a ' (a quote which is escaped using a backslash)

We do not want to do this because we dont want to print a quote, but want to print a backslash character. this is done by escaping a backslash using a backslash (so you end up with a double backslash) \

So the correct B array should be:

B = [' _ ', '|_\\', '|_/']

You also appear to have made a similair mistake in D which should be D = [' _ ', '| \'', '|_/']

Sign up to request clarification or add additional context in comments.

1 Comment

just out of curiosity, is this A, B, C, D huge character printing coming from a tutorial?
0

You have to escape the escape character \' results ' as string and \\ results \ as string:

B = [' _ ', '|_\\', '|_/']
n = len(B)
for i in range(n):
    print(B[i])

4 Comments

Ohh it prints it fine this way, but when I print it with just a print(B) I get the bad result
Maybe print(''.join(B))
No @RocketNikita, he wants them printed underneath eachother so they draw a big ascii B character, he should do something like print("\n".join(B)) take a look at my answer
Tamás, yes because print(B) is not putting a new line after each element. I suggest you to use for cycle instead of join method. It's more readable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.