开发者

How do I compare a value to a backslash?

if (message.value[0] == "/" or message.value[0] == "\"):
    do stuff.

I'm sure it's a simple syntax error, but something is开发者_如何学Python wrong with this if statement.


When you only need to check for equality, you can also simply use the in operator to do a membership test in a sequence of accepted elements:

if message.value[0] in ('/', '\\'):
    do_stuff()


Escape the backslash:

if message.value[0] == "/" or message.value[0] == "\\":

From the documentation:

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.


Try like this:

if message.value[0] == "/" or message.value[0] == "\\":
  do_stuff


If message.value[] is string:

if message.value[0] in ('/', '\'):
    do_stuff()

If it not str


Use following code to perform if-else conditioning in python: Here, I am checking the length of the string. If the length is less than 3 then do nothing, if more then 3 then I check the last 3 characters. If last 3 characters are "ing" then I add "ly" at the end otherwise I add "ing" at the end.

Code-

if (len(s)<=3):
    return s
elif s[-3:]=="ing":
    return s+"ly"
else: return s + "ing"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜