6

I have used Python occasionally for several months, I know we can use # and """ or ''' to comment. But when i wanted to comment some items of a dictionary, with comment words ('''), i failed.

testItems = {
'TestOne':
{
    "NameId":101
    "Score":99
 },

'''
 'TestTwo':
 {
    "NameId":101
    "Score":99
 }
'''
}

then i get ther error of SyntaxError: invalid syntax pointing to the last ''' position.

I also know there are some indent rule of python language. But i tried so many indent possibility, still fail.

enter image description here

13
  • 10
    Multiline strings are not comments. Commented Jun 1, 2016 at 5:31
  • 2
    If you remove ,, then it is syntactic, but it won't mean what you think. As @IljaEverilä says, """ is not a comment but a multiline string; two strings next to each other are syntactically equivalent to a string literal that is their concatenation: "a" "b" == "ab". Thus, { 'a': 'b' """ 'c': 'd' """ } is equivalent to {'a': "b 'c': 'd' "}. Commented Jun 1, 2016 at 5:33
  • 2
    @HassanMehmood: No, they are strings. Any value evaluated as a statement is ignored: 1, "foo", """ bar """. But this is not a statement, it is inside another literal, and cannot be ignored. Commented Jun 1, 2016 at 5:35
  • 1
    @HassanMehmood that does not make it a comment. It is a multiline string expression, but the result is not bound to a name or used in any way. Commented Jun 1, 2016 at 5:36
  • 1
    @IljaEverilä, "triple-quotes are a way to insert text that doesn't do anything (I believe you could do this with regular single-quoted strings too), but they aren't comments - the interpreter does actually execute the line (but the line doesn't do anything). That's why the indentation of a triple-quoted 'comment' is important. " from the link you shared, this comment from Demismade more sense than most of the answers Commented Jun 1, 2016 at 5:38

3 Answers 3

3

You can only use ''' or """ to comment where strings are allowed as these don't create comments, but just strings.

In the situation you describe, you are not allowed to put a string. Either move the closing } upwards or uncomment your unwanted code part line by line.

Doing

test_items_1 = {
    "NameId":101,
    "Score":99
 }

test_items_2 = {
    "NameId":101,
    "Score":99
}

testItems = {
    'TestOne': test_items_1,
#    'TestTwo': test_items_2,
}

would work as well.

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

3 Comments

There should be a comma between the "NameId" and "Score" key-value pairs in the first two dictionaries.
@KevinJ.Chase Thank you! Just fixed it.
Come to think of it, "you are not allowed to put a string" isn't right either... Strings are valid dictionary keys. The problem is that it's not followed by a colon and a value. See Prabhakar's answer.
3

Values in between ''' or """ within dictionary will be considered as another item, not comment.

In your case the the content between ''' is treated as another item's key in that dictionary.

You have to use # to comment the unwanted codes.

Ex:

testItems = {
'TestOne':
{
    "NameId":101,
    "Score":99
 },

# 'TestTwo':
# {
#    "NameId":101
#    "Score":99
# }

}

1 Comment

This is the first answer to explicitly state the problem: The syntax error has nothing to do with what's inside the string, or even the fact that it's a string --- the problem is that the multi-line dictionary key ''' 'TestTwo': {...} ''' is not followed by a colon and a value. That's why his IDE drew the red squiggly underline where the : should have appeared.
1

As Ilja Everilä mentioned, there is no multi-line comment in python. Although when I copied your code to my pycharm templet, It didn't gave me any error. Still, in your case, I'd recommend you to use single line commenting method (#). Still, '''...''' or """..."""(convert that part to string) could be used but it will just increase your line of code. Coming to your question, you are getting an error because:

Your code could be rewritten as :

testItems = {'TestOne': {"NameId":101, "Score":99} ''' 'TestTwo':{ "NameId":101"Score":99 } ''' }

That's how python reads it, As you want to keep the part in bold, and comment the italics part. You just can't use string. As you can see that python is taking the whole part in braces (Bold + Italics) as single entity. So either use #, or take out that part from there.

You could rewrite as:

testItems = {
    'TestOne':
        {
            "NameId":101,
            "Score":99
        }
    # 'TestTwo':
    #     {
    #         "NameId":101,
    #         "Score":99
    #     }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.