Categories
Python Answers

How to capitalize the first letter of each word in a string with Python?

Sometimes, we want to capitalize the first letter of each word in a string with Python.

In this article, we’ll look at how to capitalize the first letter of each word in a string with Python.

How to capitalize the first letter of each word in a string with Python?

To capitalize the first letter of each word in a string with Python, we can use the string’s title method.

For instance, we write:

s = "hello world".title()
print(s)

We call title on "hello world" to return the string with the first letter of each word capitalized.

And then we assign the returned string to s.

Therefore, s is 'Hello World'.

Conclusion

To capitalize the first letter of each word in a string with Python, we can use the string’s title method.

Categories
Python Answers

How to compute list difference with Python?

Sometimes, we want to compute list difference with Python.

In this article, we’ll look at how to compute list difference with Python.

How to compute list difference with Python?

To compute list difference with Python, we can use the - operator with sets.

For instance, we write:

A = [1, 2, 3, 4]
B = [2, 5]

x = list(set(A) - set(B))
print(x)

We create sets from A and B with the set functions.

Then we get the difference between the 2 sets with the - operator.

Finally, we convert the returned set difference back to a list with list and assign that to x.

Therefore, x is [1, 3, 4].

Conclusion

To compute list difference with Python, we can use the - operator with sets.

Categories
Python Answers

How to round up a number with Python?

Sometimes, we want to round up a number with Python.

In this article, we’ll look at how to round up a number with Python.

How to round up a number with Python?

To round up a number with Python, we can use the math.ceil method.

For instance, we write:

import math

print(int(math.ceil(5.2)))

Then we see 6 is printed since we call math.ceil to round 5.2 up to the nearest integer.

And then we use int to convert the float returned by math.ceil to an integer.

Conclusion

To round up a number with Python, we can use the math.ceil method.

Categories
Python Answers

How to generate set partitions in Python?

Sometimes, we want to generate set partitions in Python.

In this article, we’ll look at how to generate set partitions in Python.

How to generate set partitions in Python?

To generate set partitions in Python, we can use the more_itertools module.

For instance, we write:

pip install more-itertools

Then we write:

import more_itertools as mit

lst = [1, 2, 3]
partitions = [
    part for k in range(1,
                        len(lst) + 1) for part in mit.set_partitions(lst, k)
]
print(partitions)

We use list comprehension with mit.set_partitions(lst, k) to create partitions by dividing it with k as the boundary.

It returns the part list which has the partition with k as the boundary.

And we return a nested list with all the partitions with different values of k from 1 to len(lst).

Therefore, partitions is [[[1, 2, 3]], [[1], [2, 3]], [[1, 2], [3]], [[2], [1, 3]], [[1], [2], [3]]].

Conclusion

To generate set partitions in Python, we can use the more_itertools module.

Categories
Python Answers

How to replace non-ASCII characters with a single space in Python?

Sometimes, we want to replace non-ASCII characters with a single space in Python.

In this article, we’ll look at how to replace non-ASCII characters with a single space in Python.

How to replace non-ASCII characters with a single space in Python?

To replace non-ASCII characters with a single space in Python, we can use the unidecode module.

To install it, we run:

pip install unidecode

Then we use it by writing:

from unidecode import unidecode


def remove_non_ascii(text):
    return unidecode(text)


n = remove_non_ascii(u"Ceñía")
print(n)

We have the remove_non_ascii function that takes the text string.

Then we call unideocde with text to return an ASCII string.

Next, we call remove_non_ascii function with the u"Ceñía" unicode string.

Therefore, n is 'Cenia'.

Conclusion

To replace non-ASCII characters with a single space in Python, we can use the unidecode module.