首页 > 解决方案 > Defining in-line comments in Python

问题描述

Python has three types of comments: full-line comments, in-line comments and multi-line comments.

# Prints: "Hello World!"
print("Hello World!")


print("Hello World!") # Prints: "Hello World!"


"""
Prints:
Hello World!
"""
print("Hello World!")

Now, if you would define your own comments, it would be easy to use rstrip() to ignore a line with an if statement where you check if your line starts with a character of your choice (|| for this example). But, that checks if a line starts with ||. How would track your line to the point where the comment is placed, and then ignore the rest of the line?

|| Right now, only this works.


...    || This doesn't.

Edit: Came up with a way myself.

line=(line.split("||")[0])

标签: python-3.xcomments

解决方案


这是答案:

line=(line.split("||")[0])

推荐阅读