Very new to python, so ignorance may be shown. (Python 3.10, Windows 10)
I have a simple code that tosses a coin. I want to underline the 'y' and 'n' in "yes" and "no" within this sentence: Do you want to flip another coin? yes or no.
I was able to make it work properly in IDLE, but it doesn't work properly when running in windows. The underline positions shifts to the left in Windows. I assume this is an issue in Windows, but can it be fixed? I'm simply double clicking the python file to run it in Windows, so maybe this is an incorrect way of doing it?
Here is the code for the sentence:
Yes = ('''es''')
No = ('''o''')
print('Do you want to flip another coin? ' + '\u0332y'+Yes + ' or' + ' \u0332n'+No)
Thanks for the help.
I have the solution,
follow the below code,
from prompt_toolkit import print_formatted_text, HTML
print_formatted_text('Do you want to flip another coin? ',HTML('<u>y</u>es'),'or',HTML('<u>n</u>o'))
I'm very new to coding so I decided to try Python and downloaded Visual Studio, but I am not sure how you can comment out multiple lines of code.
Normally in Visual Studio Community 2019, multi line comments are done by using CTRL+K CTRL+C and CTRL+K CTRL+U to remove comments. But this is unavailable when using python for some reason.
Personally, for learning to write Python I would recommend using Visual Studio Code, instead of Visual Studio. VS Code is a much more light weight package (install size approx 300MB compared to several GB) This means that it opens in a fraction of the time.
In VS Code the Python extension will provide all of your python needs and allow you to toggle block comments by using CTRL+/
In python, you can use triple quotes to comment multiple lines of code. Example:
"""
comment
still comment
"""
print('Hello world!')
The text in the quotes is commented, but the print statement below it is not and will work.
there is a short cut for most of the languages in VS code :
Ctrl+/
edit: you need to select the lines that you intend to comment out
to comment out multiple lines in python you can use triple quotes like this:
"""
this is line 1 of my comment
and this is line 2 of my comment
this is link 3
"""
you get the point as long as there are triple quotes at the beginning of the first line and at the end of the last line
Ctrl + Shift + A did the job for me. It will put you """ at the beginning and at the end of your selected text.
In my case, I use the shortcut key ctrl + k and ctrl + c to comment. To uncomment just hit the ctrl + k and ctrl + u
Clicking the edit button in Visual Studio at the bottom is how you can comment lines out.
"toggle line comment" for me done with ctrl + ',
but could be something else for you
"toggle block comment" for me is done with shift + alt + A,
but could be something else for you
In both cases does the first click comment and clicking again uncomments
I was wondering, if there is a PRO way of commenting/removing multiline # comments in JupyterNotebooks.
# line1
# line2
# line3
Something like SHIFT + " for adding triple quotes.
CTRL+/ for comment and uncomment multiple lines
you can press 'h' anywhere in command mode, you can find all the shortcuts of jupyter.
For the big bunch of people that does not use an english keyboard, probably the adequate keys are in other place.
In the case of a Spanish Keyboard, I can comment/uncomment lines using "Ctrl + }"
On a German keyboard layout I use CMD + ? successfully.
For those with a Portuguese keyboard and Windows operating system, CTRL + ~ comments multiple lines.
Whats is the keyboard short cut to comment single line of code and slsected lines of code in Thony IDE for python?
Ie, The Thony equivalent of ctrl + / in VS Code
Single line comment.Ctrl + 1.
Multi-line comment select the lines to be commented. Ctrl + 4.
Unblock Multi-line comment. Ctrl + 5.
To comment single line:
Put a cursor on the line which you want to comment. Press Alt+3 to comment, Alt+4 to un-comment.
To comment single line:
Select the desired lines of code to comment. Follow same keys i.e. press Alt+3 to comment, Alt+4 to un-comment.
Note:
For further shortcuts, please click on Edit menu option next to File and you'd see various shortcuts available in Thonny IDE.
Use Ctrl+3 for toggling between "commented" and "uncommented".
Use Alt+3 to comment.
Use Alt+4 to uncommment.
(Screenshot)
If you want to change the default shortcuts, you can follow the instructions on this official github-page:
https://github.com/thonny/thonny/wiki/Custom-shortcuts
With the latest update of Thonny IDE you can do
ctrl + 3
for both single line and multiline commenting and uncommenting.
This question already has answers here:
Why doesn't Python have multiline comments?
(18 answers)
Closed 9 years ago.
Is there a mechanism to comment out large blocks of Python code?
Right now, the only ways I can see of commenting out code are to either start every line with a #, or to enclose the code in triple quotes: """.
The problem with these is that inserting # before every line is cumbersome and """ makes the string I want to use as a comment show up in generated documentation.
After reading all comments, the answer seems to be "No".
Python does not have such a mechanism. Prepend a # to each line to block comment. For more information see PEP 8. Most Python IDEs support a mechanism to do the block-commenting-with-hash-signs automatically for you. For example, in IDLE on my machine, it's Alt+3 and Alt+4.
Don't use triple-quotes; as you discovered, this is for documentation strings not block comments, although it has a similar effect. If you're just commenting things out temporarily, this is fine as a temporary measure.
Hide the triple quotes in a context that won't be mistaken for a docstring, eg:
'''
...statements...
''' and None
or:
if False: '''
...statements...
'''
The only cure I know for this is a good editor. Sorry.
The only way you can do this without triple quotes is to add an:
if False:
And then indent all your code. Note that the code will still need to have proper syntax.
Many Python IDEs can add # for you on each selected line, and remove them when un-commenting too. Likewise, if you use vi or Emacs you can create a macro to do this for you for a block of code.
In JetBrains PyCharm on Mac use Command + / to comment/uncomment selected block of code. On Windows, use CTRL + /.
M-x comment-region, in Emacs' Python mode.
At least in VIM you can select the first column of text you want to insert using Block Visual mode (CTRL+V in non-windows VIMs) and then prepend a # before each line using this sequence:
I#<esc>
In Block Visual mode I moves to insert mode with the cursor before the block on its first line. The inserted text is copied before each line in the block.
In vi:
Go to top of block and mark it with letter a.
Go to bottom of block and mark it with letter b
Then do
:'a,'b s!^!#!
comm='''
Junk, or working code
that I need to comment.
'''
You can replace comm by a variable of your choice that is perhaps shorter, easy to touch-type, and you know does not (and will not) occur in your programs. Examples: xxx, oo, null, nil.
In Visual Studio using the Python Tools for Visual Studio, blocks can be commented out by Ctrl+K, Ctrl+C and uncommented by Ctrl+K, Ctrl+U.
I use Notepad++ on a Windows machine, select your code, type CTRL-K. To uncomment you select code and press Ctrl + Shift + K.
Incidentally, Notepad++ works nicely as a Python editor. With auto-completion, code folding, syntax highlighting, and much more. And it's free as in speech and as in beer!
In Eclipse + PyDev, Python block commenting is similar to Eclipse Java block commenting; select the lines you want to comment and use Ctrl + / to comment. To uncomment a commented block, do the same thing.
Yes, there is (depending on your editor). In PyDev (and in Aptana Studio with PyDev):
Ctrl + 4 - comment selected block
Ctrl + 5 - uncomment selected block
The only mechanism to comment out Python code (understood as code ignored by the interpreter) is the #.
As you say, you can also use string literals, that are not ignored by the interpreter, but can be completely irrelevant for the program execution.
In Eclipse using PyDev, you can select a code block and press Ctrl + #.
Triple quotes are OK to me.
You can use ''' foo ''' for docstrings and """ bar """ for comments or vice-versa to make the code more readable.
Another editor-based solution: text "rectangles" in Emacs.
Highlight the code you want to comment out, then C-x-r-t #
To un-comment the code: highlight, then C-x-r-k
I use this all-day, every day. (Assigned to hot-keys, of course.)
This and powerful regex search/replace is the reason I tolerate Emacs's other "eccentricities".
On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.
Use a nice editor like SciTe, select your code, press Ctrl + Q and done.
If you don't have an editor that supports block comments you can use a triple quoted string at the start and the end of your code block to 'effectively' comment it out. It is not the best practice though.