I'm using the Atom editor as I make my way through Learn Python the Hard Way.
For some reason, Atom will autogenerate closed quotes in a basic string.
x = "There are 10 types of people."
But it will not when the string starts with f.
x = f"There are {types_of_people} types of people.
When I get to the end of the f-string, I have to add the closed quotes manually, which actually generates a new set of both open and closed quotes.
x = f"There are {types_of_people} types of people.""
I then have to delete one set of quotes to make the f-string work, which is not a big deal but annoying.
I've tried putting a space between the f and the open quotes. Doing so generates the closed quotes but results in a coding error.
I haven't had this problem with the Sublime or Mu editors. Any advice on how to fix it?
The following link has a suggested solution:
F string autocomplete python
However, the solution is almost two years old. I'm wondering if there's a more recent fix that is less convoluted.
Related
This question already has answers here:
Incorrect column alignment when printing table in Python using tab characters
(4 answers)
Closed 1 year ago.
I'm learning Python through "Python Projects for Beginners" by Connor Milliken. In the first project "Creating a Recipt Printing Program" there is this section
# creating a product and price for three itens
p1_name, p1_price = "Books", 49.95
p2_name, p2_price = "Computer", 579.99
p3_name, p3_price = "Monitor", 124.89
# create a print statement for each product
print("\t{}\t\t${}".format(p1_name.title(), p1_price))
print("\t{}\t\t${}".format(p2_name.title(), p2_price))
print("\t{}\t\t${}".format(p3_name.title(), p3_price))
The lines are equal but for the second line the price is misaligned as if it has another \t. The problem was the same in jupyter notebook and Atom + terminal. If you just delete one '\t' the problem is solved but you can't really understand what happened.
Don't think of a tab as inserting a specific number of spaces in the string (it doesn't). Instead, you are giving control over whoever displays the string, since they are the ones that decide where the tab stops are.
If you want precise control, use fixed-width padded format specifiers instead. For example,
print(" {:>10} {:>6}".format(p1_name.title(), p1_price))
This assumes that 10 characters is wide enough for any title and 6 characters is wide enough for any price.
I often forget to prefix formatted strings with "f".
A buggy example: text = "results is {result}"
Where it should be text = f"results is {result}"
I make this error A LOT; my IDE don't report it, and the program runs without exceptions.
I thought maybe to scan my source code for quoted strings, check for {,} characters, and search if it's missing a prefix "f" literal;
But it's better, i guess, to use a parser? Or maybe someone did it already?
The problem is that text = "results is {result}" is a valid template string, so you can later use it in your program like:
>>> text.format(result=1)
'results is 1'
>>> text.format(result=3)
'results is 3'
What you can achieve is just checking if an f-string does indeed use variables inside, like pylint and flake8 already do.
For what you seek, however, there is something going on with PyLint:
this issue is 3 years old, but it is exactly what you need, and recently - three days ago - an user submitted a pull request that is currently WIP, and should resolve your problem.
This question already has answers here:
How to properly escape a double quote in CSV?
(6 answers)
Closed 6 years ago.
I am manually writing .csv files using Python/Django and then attempting to open those .csv files using Excel for Mac version 15.21.1.
I can successfully escape all of the , characters, but am unable to escape " characters.
For example...
I have a bit of data...
hell"oworld
How do I escape the " from that data? I have tried (per advice of others on SO) to use solutions such as doubling the quote like so hell""oworld and have also tried something like hell" & CHAR(34) & oworld to no avail. I understand that character escaping is tricky business, but could someone please throw me a bone?
Already tried these suggestions...
solution1
there is a lot of answers, e.g this or this one
when you escape text - it should be enclosed by quotation marks, then if you have a quotation mark in the text itself, it should be replaced by two quotation marks:
do if you have hell"oworld as your cell value, in the .csv file you shoud have:
"hell""oworld"
This question already has answers here:
Python comments: # vs. strings
(3 answers)
Closed 9 years ago.
I know two ways of leaving comments in python. One is using """ and the other is using #. I know that the first can be used to return a functions help as a benefit. But when should I use one and when the other? And also how do I have to leave comments? Do I have to press tab and arrange the first line of comment with the the command beneath it? Or do I have to start from the beginning of the line?
No, there is only one way of commenting, using #:
A comment starts with a hash character (#) that is not part of a string literal, and ends at the end of the physical line.
Triple quoting, """, creates a string object, which happens to be used as the docstring when it is the first line of a function, module or a a class. Triple quoting is useful in many other places too, but should not be confused with commenting. You can use a triple quoted string like any other string literal, with the specific benefit that you can use actual newlines in your source code instead of having to use \n escape characters.
Although it can be used to disable a block of code by turning it into a multi-line string instead, you really should not do this. Use proper source code control and simply delete the block, or use an editor that lets you comment out whole blocks by inserting # for you instead.
For actual comments, use #. The Python style guide (PEP 8) has some things to say about when and how to use commenting; it has this to say about indentation:
Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).
This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 3 years ago.
I have a simple piece of code that I'm not understanding where my error is coming from. The parser is barking at me with an Unexpected Indent on line 5 (the if statement). Does anyone see the problem here? I don't.
def gen_fibs():
a, b = 0, 1
while True:
a, b = b, a + b
if len(str(a)) == 1000:
return a
If you just copy+pasted your code, then you used a tab on the line with the if statement. Python interprets a tab as 8 spaces and not 4. Don't ever use tabs with python1 :)
1 Or at least don't ever use tabs and spaces mixed. It's highly advisable to use 4 spaces for consistency with the rest of the python universe.
You're mixing tabs and spaces. Tabs are always considered the same as 8 spaces for the purposes of indenting. Run the script with python -tt to verify.
Check if you aren't mixing tabs with spaces or something, because your code pasted verbatim doesn't produce any errors.
The line with a, b = b, a + b is indented with 8 spaces, and the if line is indented with 4 spaces and a tab. Configure your editor to never ever insert tabs ever.
(Python considers a tab to be 8 spaces, but it's easier just not to use them)
Check whether your whitespace in front of every line is correctly typed. You may have typed tabs instead of spaces - try deleting all the space and change it so you use only tabs or only spaces.