This question already has answers here:
Why is semicolon allowed in this Python snippet?
(15 answers)
Closed 6 years ago.
What's the difference with Python statements ending with ; and those does not?
There is really no difference. Python ends a line of code at the end of the logical line, or when it encounters ;
The only advantage to using ; is that you can stack multiple logical lines into one physical line. For example (in python3):
import sys
for i in range(10):
print(i, end=' '); sys.stdout.flush()
That said, this is terrible coding style, so don't ever do it
Semicolons serve the same purpose as the newline character. It is really just bad style to use a semicolon, often from people coming from languages where lines require it.
Related
This question already has answers here:
Mid-line comment in Python?
(2 answers)
Closed 1 year ago.
In languages like PHP we can do the following:
>>> echo "cat is"." not"." dead"
cat is not dead
>>> echo "cat is"/*." not"*/." dead"
cat is dead
What I have done on the second line is, commenting out a chunk of it using PHP's multi-line comment feature.
Please let me know how to do the same in Python without breaking the line of code into separate lines.
Thanks..!
Generally, no. To keep old code around, duplicate the line, comment out one, and modify the other.
This question already has answers here:
Remove Last instance of a character and rest of a string
(5 answers)
Closed 3 years ago.
I have a string such as:
string="lcl|NC_011588.1_cds_YP_002321424.1_1"
and I would like to keep only: "YP_002321424.1"
So I tried :
string=re.sub(".*_cds_","",string)
string=re.sub("_\d","",string)
Does someone have an idea?
But the first _ is removed to
Note: The number can change (they are not fixed).
"Ordinary" split, as proposed in the other answer, is not enough,
because you also want to strip the trailing _1, so the part to capture
should end after a dot and digit.
Try the following pattern:
(?<=_cds_)\w+\.\d
For a working example see https://regex101.com/r/U2QsFH/1
Don't bother with regexes, a simple
string.split('_cds_')[1]
will be enough
This question already has answers here:
How can I do a line break (line continuation) in Python?
(10 answers)
Closed 4 years ago.
I have a long expression, its' not fitting in my screen, I want to write in several lines.
new_matrix[row][element] = old_matrix[top_i][top_j]+old_matrix[index_i][element]+old_matrix[row][index_j]+old_matrix[row][index_j]
Python gives me 'indent' error if I just break line.
Is there way to 'fit' long expression in screen?
I hate backslashes, so I prefer to enclose the right hand side in parens, and break/indent on the top-level operators:
new_matrix[row][element] = (old_matrix[top_i][top_j]
+ old_matrix[index_i][element]
+ old_matrix[row][index_j]
+ old_matrix[row][index_j])
You can break expressions into multiple lines by ending each line with \ to indicate the expression will continue on the next line.
Example:
new_matrix[row][element] = old_matrix[top_i][top_j]+ \
old_matrix[index_i][element]+old_matrix[row][index_j]+ \
old_matrix[row][index_j]
Yes, use \:
new_matrix[row][element] = old_matrix[top_i][top_j]+old_matrix[index_i]\
[element]+old_matrix[row][index_j]+old_matrix[row][index_j]
This question already has answers here:
Replace console output in Python
(12 answers)
Closed 4 years ago.
I am trying to build a game.
The game will have an item called a "pulsating crystal" (I am using \033[1;31;40m] to change the items colour), I want to it to be rainbow, so it keeps changing colours, without deleting everything else in the terminal. I used print(\033c) to clear the terminal but I just want to print the last line. I am sorry if the question is unclear or repetitive, or has another answer but I couldn't find another clear answer for my problem. PS I use Linux.
I just want to print the last line.
To print a line repeatedly, just override the line ending \n by giving the keyword argument end='\r' to print().
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python, Printing multiple times,
I'd like to know how to print a string such as "String" 500 hundred times?
You can use repetition (*):
print('String' * 500)
In this way Python will first create the whole string ("StringStr...String") in memory and only then will print it.
If you don't want to use so much memory, then you can use a for loop:
for i in range(500):
print('String', end='')
print()
end='' is needed to prevent Python from printing end-of-line characters after each print. print() at the end is needed to print the end-of-line character after the last print.