Python Forum
???: if "{choice}" in self._command - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ???: if "{choice}" in self._command (/thread-29211.html)



???: if "{choice}" in self._command - BaiYouLing4 - Aug-23-2020

What does this do?

if "{choice}" in self._command
Curly braces are for replacement fields in strings for the .format method (or so I thought).

So I don't understand what this code is doing.

self._command = "M876"
choice = "0"

Thanks for any/all help


RE: ???: if "{choice}" in self._command - ndc85430 - Aug-23-2020

The string isn't an f-string, so the test is for whether the literal string "{choice}" is in self._command. Since it isn't an f-string, the value of the variable choice is irrelevant. Did you try running a complete program with that code and appropriate values?


RE: ???: if "{choice}" in self._command - Gribouillis - Aug-23-2020

BaiYouLihg4 Wrote:What does this do?
It tests if self._command contains the literal string "{choice}". If the author intended a replacement of "{choice}" by "0", he should have used f"{choice}" (for python version >= 3.6)


RE: ???: if "{choice}" in self._command - BaiYouLing4 - Aug-23-2020

Thanks for clearing up my confusion!