Categories
Python Answers

How to check if a string is empty with Python?

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

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

How to check if a string is empty with Python?

To check if a string is empty with Python, we can use the not operator.

For instance, we write:

some_string = ''
if not some_string:
    print('string is empty')

We put not before some_string to check if it’s empty.

We can do this since an empty string is falsy.

Therefore, 'string is empty' should be printed.

Conclusion

To check if a string is empty with Python, we can use the not operator.

Categories
Python Answers

How to get the class name of an instance with Python?

Sometimes, we want to get the class name of an instance with Python.

In this article, we’ll look at how to get the class name of an instance with Python.

How to get the class name of an instance with Python?

To get the class name of an instance with Python, we can use the type function and the __name__ property.

For instance, we write:

class A:
    def whoami(self):
        print(type(self).__name__)


a = A()
a.whoami()

We create the A class with the whoami method that prints the class name by printing type(self).__name__.

Therefore, we should see 'A' printed when we create a new A instance and assign it to a and then call a.whoami.

Conclusion

To get the class name of an instance with Python, we can use the type function and the __name__ property.

Categories
Python Answers

How to measure elapsed time in Python?

Sometimes, we want to measure elapsed time in Python.

In this article, we’ll look at how to measure elapsed time in Python.

How to measure elapsed time in Python?

To measure elapsed time in Python, we can use the time module.

For instance, we write:

import time

start = time.time()
print("hello world")
end = time.time()
print(end - start)

We call time.time to return the current time as a Unix timestamp and assign the result to start and end.

And then we subtract end from start to get the elapsed time in seconds.

Conclusion

To measure elapsed time in Python, we can use the time module.

Categories
Python Answers

How to generate random integers between 0 and 9 with Python?

Sometimes, we want to generate random integers between 0 and 9 with Python.

In this article, we’ll look at how to generate random integers between 0 and 9 with Python.

How to generate random integers between 0 and 9 with Python?

To generate random integers between 0 and 9 with Python, we can use the random module’s randrange function.

For instance, we write:

from random import randrange
print(randrange(10))

We call randrange with 10 to generate a random number between 0 and 9.

Conclusion

To generate random integers between 0 and 9 with Python, we can use the random module’s randrange function.

Categories
Python Answers

How to generate a random string with upper case letters and digits with Python?

Sometimes, we want to generate a random string with upper case letters and digits with Python.

In this article, we’ll look at how to generate a random string with upper case letters and digits with Python.

How to generate a random string with upper case letters and digits with Python?

To generate a random string with upper case letters and digits with Python, we can use the random.choices method.

For instance, we write:

import random
import string

N = 10

gen = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
print(gen)

to generate a 10 character random string.

We call join on an empty string with the randomly generated characters.

We generate 10 random characters with random.choices(string.ascii_uppercase + string.digits, k=N).

string.ascii_uppercase + string.digits will generate numbers and letter characters.

k is the number of characters to generate.

And an array characters is returned.

Therefore, gen should be something like 'M5HHZYW06X'.

random.choices is available since Python 3.6.

Conclusion

To generate a random string with upper case letters and digits with Python, we can use the random.choices method.