Home python At least one uppercase, lowercase character and number

At least one uppercase, lowercase character and number

Author

Date

Category

Let’s say there is a random string. If it does not contain at least one uppercase or lowercase character or number, then correct it in such a way that after the correction there will be at least one uppercase character, at least one lowercase character and any number. How can you do this without a lot of if’s, but without changing the line length?


Answer 1

Well, I got it in four ifs (in general, I could have merged everything into one huge one, but this is somehow too much). Yes, this option is quite long, but still appropriate. Here’s why: numbers and upper and lower case letters are added when necessary , not always (that is: if there is no number, then it is added). If you need it for passwords, then this option will do. Only here in else-s you need to modify it by adding random. Well, it’s not that hard anymore.

Highlights of the code:

1.Adding string
2. Checking for its length
3. Checking for the content of numbers
4.Checking for the content of small and capital letters (small letters have a slight difference in checking: since numbers are also small characters, then a comparison is made with a long list of numbers contained in the string)
5. Correcting the string in accordance with the above
checks.

string = input ('')
  if len (string) & gt; 2:
    if len ([s for s in string if s in list ('1234567890')]) & gt; 0:
      if len ([s for s in string if s.islower ()]) & gt; len ([s for s in string if s in list ('1234567890')]:
       if len ([s for s in string if s.isupper ()]) & gt; 0:
         pass
       else:
         string = list (string)
         string [0] = 'I'
         string = '' .join (string)
      else:
         string = list (string)
         string [1] = 'z'
         string = '' .join (string)
    else:
         string = list (string)
         string [2] = '8'
         string = '' .join (string)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions