How i can check if char is '\'?
-
-
@Dl10 said in How i can check if char is '\'?:
Hi,
How I can check if char is '' in python?
I add a pitcher.
Help, tnx!-
As @mrjj said, just as you have correctly doubled the
\
to\\
in your second line, you must double it to\\
in the first line. This is true whether you include it inside'
s or"
s. -
Although your use of
is
here does work, personally I would not use it in this context and would always use==
instead. See, say, https://stackoverflow.com/a/22885981/489865, which you should read & understand for howis
actually works. -
Purely as a matter of taste, the
+=
operator is defined for strings and does an append. I find that clearer/less error-prone than repeating the variable name on the right-hand side of the assignment.
So personally I would write your code as:
if char == '\\': file_ += '\\'
-
-
@SGaist said in How i can check if char is '\'?:
To add to @JonB:
file_ += char
would be even cleaner.I left it using the literal rather than the "variable", as it's not obvious from the context whether OP really wants the same char he's testing for appended or always a
\
, if you see what I mean!