To check for empty string after a portion of the string in python 3.6

Cameron Simpson cs at
Mon Dec 3 16:29:53 EST 2018

Note: post returned to the tutor list.

Please DO NOT cross post to multiple lists (i.e. tutor and python-list, 
as you have). This makes things difficult for people who are not on both 
lists. Pick a _single_ list, and use that.

On 04Dec2018 02:46, srinivasan <srinivasan.rns at> wrote:
>Could you please help me, as am still learning python syntax, how can
>I add conditional check for empty string after running "hcitool scan"
>(ie., when there is no Bluetooth devices discovered) ie., after the
>word  "Scanning..." , when there are no Bluetooth discover-able
>devices and then return False in the below python code snippet?
>~$ sudo hcitool scan
>Scanning ...                              -------------------------->
>When there are no BT devices
>~$ sudo hcitool scan
>Scanning ...
>64:A2:F9:06:63:79 OnePlus 6 ---------------------------> When there
>are BT devices
>Python code snippet:
>    def bluetooth_scan(self):
>        """
>            Start bluetooth scanning process.
>            :return: Return device info by mac address and name.
>        """
>        cmd = "sudo hciconfig hci0 up"
>        self._helper.execute_cmd_return_code(cmd)
>        cmd = "sudo hcitool scan"
>        res = self._helper.execute_cmd_output_string(cmd)
>        return res

Well you document your function as returning device info by MAC. So part 
the output if "hcitool scan" and for MAC address and name and store that 
in a dictionary (key MAC, value the name). Return the dictionary.

If the dictionary is empty, there were no devices.

Not that like almost all collections, empty dictionaries are "false", so 
you can go:

  bluetooth_devices = scanner.bluetooth_scan()
  if not bluetooth_devices:
    ... no devices found ...

Cameron Simpson <cs at>

More information about the Python-list mailing list