String constants in python

The string module has some of the builtin constant variables. We can use this instead of declaring the constants again in our code. Some of the string constants that will be discussed here are,

  1. ascii_letters
  2. ascii_lowercase
  3. ascii_uppercase
  4. octdigits
  5. punctuation
  6. whitespace

Let us look at these constants one by one with a code snippet printing each of these string constants.

1. ascii_letters

The ascii_letters constant is the concatenation of both lowercase and uppercase alphabets as a string. Let us import the string module and print this constant.

ascii_letters in string

2. ascii_lowercase

The ascii_lowercase constant consists of all the lower case alphabets together as a string. Let us import the string module and print this constant.

ascii_lowercase

3. ascii_uppercase

The ascii_uppercase constant consists of all the upper case alphabets together as a string. Let us import the string module and print this constant.

ascii_uppercase

4. octdigits

The octdigits constant will print all the digits that are allowed in an octal number which is numbers from 0 to 7. Let us import the string module and print this constant.

octdigits

5. punctuation

The punctuation constant has a string of punctuations. Let us import the string module and print this constant.

punctuation

6. whitespace

A string containing all characters that are considered whitespace. On most systems, this includes the characters space, tab, linefeed, return, formfeed, and vertical tab.

whitespace

Conclusion

If you want to remove punctuations from your code or check if all alphabets exist in your code or something like that we can use these constants.

For more methods and constants visit the official string documentation here.

Happy coding !