Nerd For Tech
Published in

Nerd For Tech

String constants in python

Photo by Hitesh Choudhary on Unsplash

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,

  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.

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!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Allwin Raju

Allwin Raju

Python Developer | Django Developer | Python Enthusiast