Categories
Python Answers

How to do currency formatting in Python?

Sometimes, we want to do currency formatting in Python.

In this article, we’ll look at how to do currency formatting in Python.

How to do currency formatting in Python?

To do currency formatting in Python, we can use the locale.setlocale method.

For instance, we write:

import locale

locale.setlocale(locale.LC_ALL, '')
c = locale.currency(188518982.18, grouping=True)
print(c)

We call locale.setlocale to set the locale of the script by getting the locale settings from the operating system by setting it to locale.LC_ALL.

Then we call locale.currency with the float to format into a currency string.

And we set grouping to True to group the digits with the digits separator according to the locale.

Therefore, c is $188,518,982.18 given that the OS’ locale is 'English_United States.1252'.

Conclusion

To do currency formatting in Python, we can use the locale.setlocale method.

Categories
Python Answers

How to decode a UTF-8 URL string in Python?

Sometimes, we want to decode a UTF-8 URL string in Python.

In this article, we’ll look at how to decode a UTF-8 URL string in Python.

How to decode a UTF-8 URL string in Python?

To decode a UTF-8 URL string in Python, we can use the unquote function from the urllib.parse module.

For instance, we write:

from urllib.parse import unquote

url = 'example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0'
decoded = unquote(url)
print(decoded)

to decode the url with unquote and assign the decoded URL string to decoded.

Therefore, decoded is 'example.com?title=правовая+защита'.

Conclusion

To decode a UTF-8 URL string in Python, we can use the unquote function from the urllib.parse module.

Categories
Python Answers

How to check if a word is in a string with Python?

Sometimes, we want to check if a word is in a string with Python.

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

How to check if a word is in a string with Python?

To check if a word is in a string with Python, we can use the in operator.

For instance, we write:

if 'seek' in 'those who seek shall find':
    print('Success!')

to check if 'seek' is included in 'those who seek shall find'.

Since this is True, 'Success!' is printed.

Conclusion

To check if a word is in a string with Python, we can use the in operator.

Categories
Python Answers

How to add days to a date in Python?

Sometimes, we want to add days to a date in Python.

In this article, we’ll look at how to add days to a date in Python.

How to add days to a date in Python?

To add days to a date in Python, we can use the datetime.timedelta method and the + operator.

For instance, we write:

import datetime

start_date = '10/01/2021'
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%Y")

end_date = date_1 + datetime.timedelta(days=10)
print(end_date)

We call datetime.datetime.strptime with start_date and the format string to convert the date string to a date object.

%m is the 2 digit month’s format code.

%d is the 2 digit day’s format code.

%Y is the 4 digit year’s format code.

Then we add 10 days to date_1 by using datetime.timedelta(days=10) to create the 10 days time delta object.

And then we use the + operator to add 10 days to date_1.

So end_date is 2021-10-11 00:00:00.

Conclusion

To add days to a date in Python, we can use the datetime.timedelta method and the + operator.

Categories
Python Answers

How to make a comma-separated string from a list of strings in Python?

Sometimes, we want to make a comma-separated string from a list of strings in Python.

In this article, we’ll look at how to make a comma-separated string from a list of strings in Python.

How to make a comma-separated string from a list of strings in Python?

To make a comma-separated string from a list of strings in Python, we can use the string’s join method.

For instance, we write:

my_list = ['a', 'b', 'c', 'd']
my_string = ','.join(my_list)
print(my_string)

We call ''.join with my_list to join each entry in my_list with commas.

Therefore, my_string is 'a,b,c,d'.

Conclusion

To make a comma-separated string from a list of strings in Python, we can use the string’s join method.