Skip to content

Removing common parts of strings #137

@Sinus44

Description

@Sinus44

Hi. The idea arose to make the common part of the string a constant. That is, if part of the string is repeated in the code, then you should put it into a constant and then insert it using string formatting. I also want to note that it is worth doing this optionally because it affects performance and RAM consumption. For example:

def test(name):
    return f"Hello, {name}!"

name = input("Enter your name: ")
if name != "":
    print(test(name))
else:
    print("Please enter your name")

it can be reduced to the form:

A='nter your name: '
def test(name):
    return f"Hello, {name}!"

name = input("E%s"%A)
if name != "":
    print(test(name))
else:
    print("Please e%s"%A)

In this example, the difference is only 1 byte. And this example is needed solely to show how I see it. In other cases, the difference in occupied space may be greater.
The example is taken from one of the issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions