Categories
Python Answers

How to convert a string to lower case in Python?

Sometimes, we want to convert a string to lower case in Python.

In this article, we’ll look at how to convert a string to lower case in Python.

How to convert a string to lower case in Python?

To convert a string to lower case in Python, we can call the string’s lower method.

For instance, we write:

s = "Kilometer"
print(s.lower())

We call s.lower to return "Kilometer" with all lower case characters.

Therefore, 'kilometer' is printed.

Conclusion

To convert a string to lower case in Python, we can call the string’s lower method.

Categories
Python Answers

How to merge a list of dicts into a single dict with Python?

Sometimes, we want to merge a list of dicts into a single dict with Python.

In this article, we’ll look at how to merge a list of dicts into a single dict with Python.

How to merge a list of dicts into a single dict with Python?

To merge a list of dicts into a single dict with Python, we can use reduce function from the functools module.

For instance, we write:

from functools import reduce

list_of_dicts = [{'a': 1}, {'b': 2}, {'c': 1}, {'d': 2}]
d = reduce(lambda a, b: dict(a, **b), list_of_dicts)
print(d)

We call reduce with a function that merges dictionary a with the entries in b and return it.

a and b are both entries in list_of_dicts.

The merging is done by unpacking the entries in b and putting it into a new dictionary with a and returning it.

Therefore, d is {'a': 1, 'b': 2, 'c': 1, 'd': 2}.

Conclusion

To merge a list of dicts into a single dict with Python, we can use reduce function from the functools module.

Categories
Python Answers

How to convert a hexadecimal string to byte array in Python?

Sometimes, we want to convert a hexadecimal string to byte array in Python.

In this article, we’ll look at how to convert a hexadecimal string to byte array in Python.

How to convert a hexadecimal string to byte array in Python?

To convert a hexadecimal string to byte array in Python, we can use the bytes.fromhex method.

For instance, we write:

hex_string = "deadbeef"
s = bytes.fromhex(hex_string)
print(s)

We define the hex_string hex string.

Then we call bytes.fromhex with it as the argument and assign the returned byte array to s.

Therefore, s is b'\xde\xad\xbe\xef'.

Conclusion

To convert a hexadecimal string to byte array in Python, we can use the bytes.fromhex method.

Categories
Python Answers

How to check for valid email address with Python?

Sometimes, we want to check for valid email address with Python.

In this article, we’ll look at how to check for valid email address with Python.

How to check for valid email address with Python?

To check for valid email address with Python, we can use a regex and the regex search method to check whether a string is a valid email string.

For instance, we write:

import re


def valid_email(email):
    return bool(re.search(r"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$", email))


print(valid_email('abc@abc.com'))
print(valid_email('abc'))

to define the valid_email function to check if email is a valid email address.

We check for the characters before the @ with ^[\w\.\+\-]+. The pattern matches dots and words.

And we check for @ with \@

We check for characters after the @ with [\w]+\.[a-z]{2,3}$. The pattern matches dots and words with the domain suffix at the end.

Therefore, we should see:

True
False

printed since we have a valid and an invalid email string respectively.

Conclusion

To check for valid email address with Python, we can use a regex and the regex search method to check whether a string is a valid email string.

Categories
Python Answers

How to expand tuples into arguments with Python?

Sometimes, we want to expand tuples into arguments with Python.

In this article, we’ll look at how to expand tuples into arguments with Python.

How to expand tuples into arguments with Python?

To expand tuples into arguments with Python, we can use the * operator.

For instance, we write:

def add(a, b, c):
    return a + b + c


res = add(*(1, 2, 3))
print(res)

to unpack the tuple (1, 2, 3) with * as the arguments of add.

Therefore, a is 1, b is 2, and c is 3.

And res is 6.

Conclusion

To expand tuples into arguments with Python, we can use the * operator.