Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

Insert commant into python ''' string

I have this string

my_str = '''
Hi!
EveryOne
'''
print (my_str)

How can i add a comment into line without print it, for example

my_str = '''
Hi!  #DO NOT SHOW THIS STRING
EveryOne
'''
question from:https://stackoverflow.com/questions/65874860/insert-commant-into-python-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We have a few issues here.

1: The triple quote you're using here (''') is generally reserved for docstrings, not for strings within the code. You'll want to use either single (') or double (") quotes, and keep it on the same line.

2: The pound (#) is used to comment out code, but I personally don't know of a reason why you'd do that within a line like you have it here.

The format you'd want to go for is this:

#DO NOT SHOW THIS STRING
my_str = 'Hi!
EveryOne'
print(my_str)

Is there a specific reason you might want to add a comment like this in-line instead of outside of that specific block?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...