Categories
Python Answers

How to draw vertical lines on a given plot in Python matplotlib?

Sometimes, we want to draw vertical lines on a given plot in Python matplotlib.

In this article, we’ll look at how to draw vertical lines on a given plot in Python matplotlib.

How to draw vertical lines on a given plot in Python matplotlib?

To draw vertical lines on a given plot in Python matplotlib, we can use the axvline method.

For instance, we write

import matplotlib.pyplot as plt

plt.axvline(x=0.22)
plt.axvline(x=0.33)
plt.axvline(x=2.20)

to draw vertical lines x = 0.22, x = 0.33 and x = 2.20 with axvline.

Conclusion

To draw vertical lines on a given plot in Python matplotlib, we can use the axvline method.

Categories
Python Answers

How to do list subtraction operation with Python?

Sometimes, we want to do list subtraction operation with Python.

In this article, we’ll look at how to do list subtraction operation with Python.

How to do list subtraction operation with Python?

To do list subtraction operation with Python, we can use list comprehension.

For instance, we write

[item for item in x if item not in y]

to get the items in list x but not in list y with

item for item in x if item not in y

Conclusion

To do list subtraction operation with Python, we can use list comprehension.

Categories
Python Answers

How to find length of digits in an integer with Python?

Sometimes, we want to find length of digits in an integer with Python.

In this article, we’ll look at how to find length of digits in an integer with Python.

How to find length of digits in an integer with Python?

To find length of digits in an integer with Python, we can convert the int to a string with str and then use the len function.

For instance, we write

len(str(123))

to call str with 123 to convert it to a string.

Then we call len to get the length of the int string.

Conclusion

To find length of digits in an integer with Python, we can convert the int to a string with str and then use the len function.

Categories
Python Answers

How to remove all special characters, punctuation and spaces from string with Python?

Sometimes, we want to remove all special characters, punctuation and spaces from string with Python.

In this article, we’ll look at how to remove all special characters, punctuation and spaces from string with Python.

How to remove all special characters, punctuation and spaces from string with Python?

To remove all special characters, punctuation and spaces from string with Python, we can call re.sub with a regex that matches all the charaters we’re looking for and replace them with empty strings.

For instance, we write

re.sub('[^A-Za-z0-9]+', '', my_string)

to call re.sub with a regex that matches all special characters, punctuation and spaces.

Then we use an empty string as the 2nd argument to replace all the matched characters in my_string with empty strings.

Conclusion

To remove all special characters, punctuation and spaces from string with Python, we can call re.sub with a regex that matches all the charaters we’re looking for and replace them with empty strings.

Categories
Python Answers

How to convert dictionary to JSON with Python?

Sometimes, we want to convert dictionary to JSON with Python.

In this article, we’ll look at how to convert dictionary to JSON with Python.

How to convert dictionary to JSON with Python?

To convert dictionary to JSON with Python, we can use the json.dumps method.

For instance, we write

import json

r = {'is_claimed': 'True', 'rating': 3.5}
r = json.dumps(r)

to call json.dumps with dict r to convert the dict into a JSON string.

Conclusion

To convert dictionary to JSON with Python, we can use the json.dumps method.