Categories
Python Answers

How to do a case-insensitive string comparison with Python?

Sometimes, we want to do a case-insensitive string comparison with Python.

In this article, we’ll look at how to do a case-insensitive string comparison with Python.

How to do a case-insensitive string comparison with Python?

To do a case-insensitive string comparison with Python, we can convert both strings we want to compare to lower case.

For instance, we write:

string1 = 'Hello'
string2 = 'hello'

if string1.casefold() == string2.casefold():
    print("The strings are the same (case insensitive)")
else:
    print("The strings are NOT the same (case insensitive)")

We call the casefold method on both strings and compare them with ==.

Therefore, we should see 'The strings are the same (case insensitive)' printed since both strings are the same ignoring the case.

Conclusion

To do a case-insensitive string comparison with Python, we can convert both strings we want to compare to lower case.

Categories
Python Answers

How to decode HTML entities in a Python string?

Sometimes, we to decode HTML entities in a Python string.

In this article, we’ll look at how to decode HTML entities in a Python string.

How to decode HTML entities in a Python string?

To decode HTML entities in a Python string, we can use the Beautiful Soup library.

To install it, we run:

pip install bs4

Then we write:

from bs4 import BeautifulSoup

html = BeautifulSoup("<p>&pound;682m</p>")
print(html)

We instantiate the BeautifulSoup class with a string with some HTML entities in it.

Then we assign the returned object to html.

Therefore, html is '<p>£682m</p>'.

Conclusion

To decode HTML entities in a Python string, we can use the Beautiful Soup library.

Categories
Python Answers

How to print lists as tabular data with Python?

Sometimes, we want to print lists as tabular data with Python.

In this article, we’ll look at how to print lists as tabular data with Python.

How to print lists as tabular data with Python?

To print lists as tabular data with Python, we can use the tabulate library.

We install it by running:

pip install tabulate

For instance, we write:

from tabulate import tabulate
print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))

We import the tabulate function from the tabulate module.

Then we call tabulate with a nested list and set the headers parameter to a list of headers for each row.

Therefore, we get:

Name      Age
------  -----
Alice      24
Bob        19

printed as a result.

Conclusion

To print lists as tabular data with Python, we can use the tabulate library.

Categories
Python Answers

How to find all files in a directory with extension .txt in Python?

Sometimes, we want to find all files in a directory with extension .txt in Python.

In this article, we’ll look at how to find all files in a directory with extension .txt in Python.

How to find all files in a directory with extension .txt in Python?

To find all files in a directory with extension .txt in Python, we can use the glob.glob method.

For instance, we write:

import glob

for file in glob.glob("*.txt"):
    print(file)

We call glob.glob with '.txt' to find all the files with a path that ends with .txt

Then, we loop through the returned list with the for loop and print the file path in the loop body.

Conclusion

To find all files in a directory with extension .txt in Python, we can use the glob.glob method.

Categories
Python Answers

How to find the index of an item in a list with Python?

Sometimes, we want to find the index of an item in a list with Python.

In this article, we’ll look at how to find the index of an item in a list with Python.

How to find the index of an item in a list with Python?

To find the index of an item in a list with Python, we can use the Python list’s index method.

For instance, we write:

print([1, 1].index(1))

Then we print the first index of 1 in the list.

Therefore, 0 is printed.

Conclusion

To find the index of an item in a list with Python, we can use the Python list’s index method.