Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force string format in pylint

Tags:

python

pylint

Python allows to use either single or double quotes for strings. I'd like to enforce only single quotes format in my projects.

Is there any specific rule in pylint or an existing pylint plugin to achieve that?

like image 475
pablomolnar Avatar asked Apr 08 '16 01:04

pablomolnar


People also ask

What is f {} in Python?

“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with f , which contains expressions inside braces.

What is af string?

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces.

How do you use f-string and format or replacement operator in Python?

When using f-Strings to display variables, you only need to specify the names of the variables inside a set of curly braces {} . And at runtime, all variable names will be replaced with their respective values.

How do you make an F-string in Python?

To write an f-string in Python, simply preface a string with an f (or F). You can use single quotes, double quotes, or even triple quotes to create your string. Any expression you want to evaluate is placed into curly braces “{}”.


2 Answers

I recently wrote a pylint plugin for this: https://pypi.python.org/pypi/pylint-quotes

You can get it with

pip install pylint-quotes

Then to use it with pylint,

pylint --load-plugins pylint_quotes <module-or-package>

in the .pylintrc file, you can configure which quotes to use:

# Set the linting for string quotes
string-quote=single
triple-quote=double
docstring-quote=double
like image 65
edaniszewski Avatar answered Sep 20 '22 13:09

edaniszewski


I don't know if pylint can currently do this, but there is an extension for flake8 called flake8-quotes that does it.

like image 23
The Compiler Avatar answered Sep 20 '22 13:09

The Compiler