androidpythonshellmonkeyrunnermonkey

adb - How to remove the invisible new line character from a Python variable?


I use the MonkeyRunner tool which provides an API in order to manage an Android emulator or device through a script written in Python. I have assigned a process_id of an application using pid = device.shell('pidof [s] PACKAGE_NAME') command. When I concatenate the pid with another string and print the result, I get a string which has 2 lines. I need to remove the reason for breaking new line from the variable pid. How can I trim the result in order to get the raw data of pid?


Solution

  • Use the string method strip(). It will remove whitespace characters from either end of the string.

    pid = device.shell('pidof [s] PACKAGE_NAME').strip()