Categories
Python Answers

How to convert datetime object to a string of date only in Python?

Sometimes, we want to convert datetime object to a string of date only in Python.

In this article, we’ll look at how to convert datetime object to a string of date only in Python.

How to convert datetime object to a string of date only in Python?

To convert datetime object to a string of date only in Python, we can use the datetime strftime method.

For instance, we write

import datetime

t = datetime.datetime(2022, 2, 23, 0, 0)
s = t.strftime('%m/%d/%Y')

to create a datetime object with

t = datetime.datetime(2022, 2, 23, 0, 0)

Then we call strftime with the datetime format we want to return the string as to return a datetime string with the specified format.

%m is the 2 digit month.

%d is the 2 digit date.

And %Y is the 4 digit year.

Conclusio

To convert datetime object to a string of date only in Python, we can use the datetime strftime method.

Categories
Python Answers

How to removing white space around a saved image with Python matplotlib?

Sometimes, we want to removing white space around a saved image with Python matplotlib

In this article, we’ll look at how to removing white space around a saved image with Python matplotlib

How to removing white space around a saved image with Python matplotlib?

To removing white space around a saved image with Python matplotlib, we call plt.savefig with the bbox_inches argument set to 'tight'.

For instance, we write

plt.savefig('myfile.png', bbox_inches="tight")

to call savefig to save the flots to myfile.png.

We set bbox_inches to 'tight' to reduce padding between the plots.

Conclusion

To removing white space around a saved image with Python matplotlib, we call plt.savefig with the bbox_inches argument set to 'tight'.

Categories
Python Answers

How to append multiple values for one key in a dictionary with Python?

Sometimes, we want to append multiple values for one key in a dictionary with Python.

In this article, we’ll look at how to append multiple values for one key in a dictionary with Python.

How to append multiple values for one key in a dictionary with Python?

To append multiple values for one key in a dictionary with Python, we can set the value for a key to a list and the append to the list.

For instance, we write

years_dict = dict()

for line in list:
    if line[0] in years_dict:
        years_dict[line[0]].append(line[1])
    else:
        years_dict[line[0]] = [line[1]]

to loop through the list list with a for loop.

In it, we check if line[0] is a key in the years_dict dict.

If it is, then we call append to append line[1] into the list.

Otherwise, we assign a list with line[1] in it as the value of the entry with key line[0].

Conclusion

To append multiple values for one key in a dictionary with Python, we can set the value for a key to a list and the append to the list.

Categories
Python Answers

How to check if a word is an English word with Python?

Sometimes, we want to check if a word is an English word with Python.

In this article, we’ll look at how to check if a word is an English word with Python.

How to check if a word is an English word with Python?

To check if a word is an English word with Python, we can use the PyEnchant library.

To install it, we run

pip install pyenchant

Then we use it with

import enchant

d = enchant.Dict("en_US")
is_us_english =  d.check("Hello")

to create a Dict object by using the enchant.Dict class with the locale for the dictionary.

Then we call d.check with the word we want to check if it’s English or not.

Conclusion

To check if a word is an English word with Python, we can use the PyEnchant library.

Categories
Python Answers

How to check if one of the following items is in a list with Python?

Sometimes, we want to check if one of the following items is in a list with Python.

In this article, we’ll look at how to check if one of the following items is in a list with Python.

How to check if one of the following items is in a list with Python?

To check if one of the following items is in a list with Python, we can use the any function.

For instance, we write

a = [1,2,3,4]
b = [2,7]
any(x in a for x in b)

to check if any element in list b is also in a.

We call any with the the iterator that has the items in b that’s also in a with x in a for x in b to do the check.

Conclusion

To check if one of the following items is in a list with Python, we can use the any function.