1

So let's say I have a string like so :

foo = '''
some
important
stuff
'''

Is it possible to comment something inside it ? for instance :

foo = '''
some 
# important
stuff
'''
2
  • 4
    No. ''' is a multi-line string. Anything inside it is part of the string, even lines starting with # Commented Oct 20, 2022 at 8:22
  • 1
    In what situation are you? why do you need that ? Commented Oct 20, 2022 at 8:24

5 Answers 5

2

No. ''' is a multi-line string. Anything inside it is part of the string, even lines starting with #.

If you're in a situation where you need to quickly comment/uncomment part of the string and you don't mind using a function, you can abuse str.join:

foo = '\n'.join((
    'some',
    'important',
    'stuff',
))

print(foo)

outputs

some
important
stuff

while

foo = '\n'.join((
    'some',
#     'important',
    'stuff',
))

print(foo)

outputs

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

Comments

1

It is impossible, because ''' is a multi-line string. Everything inside it is a string. If you explain why you need it I can think of an other solution for your problem.

1 Comment

This should be a comment
1

Basically, no. You could do something like

f'''
one line
two line
{"# A comment"[:0]}
fourth line
'''

The '{...}' will be replaced by nothin. There will be an empty line tho.

You can remove the empty line also, but then you need a { on the line beform

f'''
one line
two line{
"#comment"[:0]}
three line
'''

Comments

0

I ran into this issue while maintaining prompts for LLMs. English is code now, and I'd like to comment the rationale behind different sections as the prompt evolves.

I made a function to drop any lines from a multi-line string that start with a # character.

def remove_comments(text):
    lines = [line for line in text.split('\n') if not line.startswith('#')]
    return '\n'.join(lines)

prompt = """You are a agent that assists with ___.
# cut unnecessary chatter at end of replies
Do not show appreciation in your responses, say only what is necessary."""

remove_comments(prompt)
# 'You are a agent that assists with ___.\nDo not show appreciation in your responses, say only what is necessary.'

Comments

-1

Comments are often outdated as they are not maintained and overlooked by the IDE.

How about a function, you still can add your comment there but at least we have some code as a foundation

def define_an_important_string() -> str:
    """
    some things here are important
    
    Returns: string with stuff (maybe important)

    """
    return '''
    some stuff
    '''

3 Comments

OP doesn't want to change a comment. They want to comment/uncomment part of a string that is actually used somewhere in the code. This does not address their issue.
I agree with that, but why do you think this function would change a comment? The idea behind it is to restructure the code to a point where you a) don't even need a comment or b) you can at least comment on the process of creating the string. On the basis of "Clean Code" by Robert C. Martin this would probably be the better option than commenting on a multiline string with no context. But in the end, you are right, it does not directly answer the question. All I'm saying is, answering the question directly might lead to "bad code".
Yet this is not what OP tries to achieve. OP wanted a way to quickly be able to comment/uncomment part of the string. "a) don't even need a comment or b) you can at least comment on the process of creating the string" is not applicable at all here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.