Python string module - codebugfree

In this section we learn the python module. String module is the one of the types of module. That is use to capable to generate the string like a to z with small letters and capital letters. It is a built in module.

We already makes the password generator tools using string module. String module has also generate the symbolic characters like #, @,$ other also.

Python string module

String module constants

The different types of string constants are:
1) string.ascii_lowercases: To generate the the lowercase alphabet a to z
2) string.ascii_uppercase: To generate the uppercase alphabet A to Z
3) string.octadigits: To generate 0 to 7
4) string.digits: To generate 0 to 9
5) string.hexidigits: To generate the number 0 to 9 and alphabet A to F
6) string.punctuation: To generate the punctuation mark or symbolic characters like #@$%    

Example of String module of python

import string
#importing the string
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.hexdigits)
print(string.digits)
print(string.octdigits)
print(string.punctuation)

INOUTPUT

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ      
0123456789abcdefABCDEF
0123456789
01234567
!"#$%&'()*+,-./:;=>[email protected][\]^_`{|}~

Practice of string module

Now we can make a practice problem the practice problem is generate the random number or digits and using string module. You can also use random module to shuffle the number.

import string
import random
#importing the string
r0 = string.ascii_lowercase
r1 = string.ascii_uppercase
r2 = string.hexdigits
r3 = string.digits
r4 = string.octdigits
r5 = string.punctuation
for randomnumber in r0,r1,r2,r3,r4,r5:
    print(randomnumber)
randomnumber = list(randomnumber)
random.shuffle(randomnumber)
print("Now printing the random number")
print(randomnumber[0:15])#it is string slicing

First we import the string and random module. random module is the module that has capable to generate any specific number or shuffle all the number. Random. shuffle string object cannot shuffle. So we use type casting of string to list. At last we use string slicing

Post a Comment

0 Comments