Categories
Python Answers

How to use quotation marks inside quotation marks with Python?

Sometimes, we want to use quotation marks inside quotation marks with Python.

In this article, we’ll look at how to use quotation marks inside quotation marks with Python.

How to use quotation marks inside quotation marks with Python?

To use quotation marks inside quotation marks with Python, we can use the single and double quotes together.

For instance, we write

print('"A word that needs quotation marks"')

to wrap our double quoted string with single quotes.

Conclusion

To use quotation marks inside quotation marks with Python, we can use the single and double quotes together.

Categories
Python Answers

How to use regular expression to return text between parenthesis with Python?

Sometimes, we want to use regular expression to return text between parenthesis with Python.

In this article, we’ll look at how to use regular expression to return text between parenthesis with Python.

How to use regular expression to return text between parenthesis with Python?

To use regular expression to return text between parenthesis with Python, we call re.search with the r'\((.*?)\)' regex string.

For instance, we write

import re

s = u'abcde(date=\'2/xc2/xb2\',time=\'/case/test.png\')'
match = re.search(r'\((.*?)\)', s).group(1)

to call re.search with the r'\((.*?)\)' regex string to and string s to find the parts of string s that has the content between parentheses.

And then we call group with 1 to get the 2nd capturing group.

Conclusion

To use regular expression to return text between parenthesis with Python, we call re.search with the r'\((.*?)\)' regex string.

Categories
Python Answers

How to hide the console when using os.system() or subprocess.call() with Python?

Sometimes, we want to hide the console when using os.system() or subprocess.call() with Python.

In this article, we’ll look at how to hide the console when using os.system() or subprocess.call() with Python.

How to hide the console when using os.system() or subprocess.call() with Python?

To hide the console when using os.system() or subprocess.call() with Python we can use STARTUPINFO .

For instance, we write

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.call('taskkill /F /IM exename.exe', startupinfo=si)

to create the si variable with subprocess.STARTUPINFO().

Then we use

si.dwFlags |= subprocess.STARTF_USESHOWWINDOW

to hide the console window.

Then we run our command with subprocess.call.

We call it with the startupinfo argument set to si.

Conclusion

To hide the console when using os.system() or subprocess.call() with Python we can use STARTUPINFO .

Categories
Python Answers

How to check if a string can be converted to float in Python?

Sometimes, we want to check if a string can be converted to float in Python.

In this article, we’ll look at how to check if a string can be converted to float in Python.

How to check if a string can be converted to float in Python?

To check if a string can be converted to float in Python, we can call float in a try block.

For instance, we write

try:
    float(element)
except ValueError:
    print("Not a float")

to call float with the value we want to check if the string can be converted to a float.

If it can’t be converted, then a ValueError will be raised.

And we catch that with the except block.

Conclusion

To check if a string can be converted to float in Python, we can call float in a try block.

Categories
Python Answers

How to sort Python list sort in descending order?

Sometimes, we want to sort Python list sort in descending order.

In this article, we’ll look at how to sort Python list sort in descending order.

How to sort Python list sort in descending order?

To sort Python list sort in descending order, we can use the sorted function.

For instance, we write

timestamps = [
    "2022-04-20 10:07:30",
    "2022-04-20 10:07:38",
    "2022-04-20 10:07:52",
    "2022-04-20 10:08:22",
    "2022-04-20 10:08:22",
    "2022-04-20 10:09:46",
    "2022-04-20 10:10:37"
]

sorted_t = sorted(timestamps, reverse=True)

to call sorted with the timestamps list with the reverse argument set to True to sort timestamps in reverse and return the new list.

Then we assign the returned sorted list to sorted_t.

Conclusion

To sort Python list sort in descending order, we can use the sorted function.