String constants in python
The string module has some of the built-in 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,
- ascii_letters
- ascii_lowercase
- ascii_uppercase
- octdigits
- punctuation
- 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.
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.
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.
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.
5. punctuation
The punctuation constant has a string of punctuations. Let us import the string module and print this constant.
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.
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 string constants.
Happy coding!