Yes, there are several ways, but first of all, when building a character-by-character string, as you do with variable
, the approach you follow is concatenating letters at the end with the operator +=
It is inefficient because of the way Python handles the strings.
Since a string for python is immutable, when you add something to it, it actually creates a new string by copying the previous string plus what you have added to it. The previous string is discarded. This repeated many times involves copying the string many times, so instead a list is used, which does allow you to add things at the end (with .append()
) instead of copying everything every time you add something.
Finally the resulting list can be converted into a chain with the operator str.join()
Now let's see different ways to solve the problem
Using lists
Basically your code, but changing the string variable
for a list:
a = '12345&&&4554444'
variable= []
for i in a:
if i!='&':
variable.append(i)
variable = "".join(variable)
print(variable)
List comprehensions ( list comprehensions )
You can use list comprehensions , which is a characteristic of the python language that allows replacing loops with a line of code.
Not only is it more compact, and in my opinion more readable although that goes with tastes, but also slightly faster:
a = '12345&&&4554444'
variable = [i for i in a if i!='&']
variable = "".join(variable)
print(variable)
Functional Programming
If you come from the Lisp world or have a mathematical mind, you may be interested in the functional paradigm, which also allows you to eliminate loops by changing them for functions that are received as iterable parameters and other functions and internally apply the function in question to each value of the iterable.
This mode does not allow anything that can not be done also with list comprehensions and in fact the Python creator prefers the comprehensions with which the functional characteristics of python (as map()
, filter()
and others) have been relegated to a separate module ( functools
) instead of being part of the language as they were in version 2.
Personally I find the syntax of the list comprehensions more elegant, but it goes with tastes. This would be the functional mode:
a = '12345&&&4554444'
variable = filter(lambda i: i!='&', a)
variable = "".join(variable)
print(variable)
In this case, filter()
expects two parameters. The second is an iterable one. The first is a function that will apply to each iterable element. If the function returns true
, it accepts the element. If you do not reject it. What it returns is another iterable one with the accepted elements (which I later convert into a string with "".join()
)
The first parameter that happened to filter()
is a lambda , which is nothing more than a kind of ultrasilver functions whose code consists only of an expression whose evaluation will be the returned value. They are written by putting the word lambda
, the name of the parameter (in this case i
), two points and the "body" of the function, which is the expression to evaluate, whose result is what will be returned. No control statement can be placed in this function. Just an expression.
String-specific functions
To substitute sub-chains, extract parts of them that follow a standard pattern, "translate" each character by a different one, etc. Python supplies many methods in class str
, and in module re
(regular expressions). I do not put here examples because there are already in other answers.