The Extract Constant refactoring makes your source code easier to read and maintain. It also helps you avoid using hardcoded constants without any explanations about their values or purpose.
Extract a Python constant in-place
The in-place refactorings are enabled in PyCharm by default. If you haven't changed this setting, the Extract Constant refactoring for Python is performed in place, right in the editor.
Place the caret within the expression or declaration of a variable to be replaced by a constant.
Do one of the following:
Choose Refactor | Extract | Constant from the main menu or the context menu.
If more than one expression is detected for the current caret position, the Expressions list appears. If this is the case, click the expression to select it. Alternatively, press Up or Down to navigate to the expression of interest, and then press Enter to select it.
If more than one occurrence of the expression is found, specify whether you wish to replace only the selected occurrence, or all the found occurrences with the new constant.
Specify the name of the constant. Select the name from the list or type the name in the box with a red border.
To complete the refactoring, press Tab or Enter.
If you haven't completed the refactoring and want to cancel the changes you have made, press Escape.
Extracting a constant using the dialog box
In the Introduce Constant Dialog dialog that opens, specify the name of the new constant.
To automatically replace all occurrences of the selected expression (if it is found more than once), select the option Replace all occurrences.
Click OK to create the constant.