1

I'm beginner to Python ... I'd like to format the characters in Python using basic concepts and operations of Tuples and Lists as below ...

  1. I enter 10 digit number and except last 4 digits remaining all the numbers should be replaced by 'X'. For e.g.

    number = 1234567890

Expecting output as -

number = XXXXXX7890

  1. How to mask entered characters / numbers in Python using Tuples/Lists concept not using by importing any modules or existing high functions. Is it possible ? For e.g. entered some characters , those should be masked using * (asterisk) or # (hashed) while entering. For e.g.

    password : pa55w0rd

Expecting output while entering password as -

password : ********

OR

password: ########

Jerry Stratton
3,5491 gold badge28 silver badges33 bronze badges
asked Aug 25, 2018 at 19:06
4
  • What have you tried already? Commented Aug 25, 2018 at 19:07
  • I learnt characters can't be edited (immutable). Was trying to create a variable assigned with '1234567890' and converting it to list and replacing the numbers using index slicing, and then with "".join() function etc.., But couldn't proceed much. Commented Aug 25, 2018 at 19:11
  • Python has a standard getpass() function for inputing passwords. Commented Aug 25, 2018 at 19:12
  • Yes, I saw the other existing posts on masking the characters with getpass() ... just wanted to know other than , is there any possibility to do masking without that getpass() Commented Aug 25, 2018 at 19:14

2 Answers 2

2

It is always better to use built-in modules for things sensitive like password. One way of doing is following:

import getpass
number = 1234567890
first = 'X' * max(0,len(str(number)[:-4]))
last = str(number)[-4:]
n = first + last
print(n)
# part 2
p = getpass.getpass(prompt='Enter the number : ')
if int(p) == 123:
 print('Welcome..!!!')
else:
 print('Please enter correct number..!!!')

If you don't want to display typed password just print:

print('######')

It does not have to be of the same length you just have to print something.

answered Aug 25, 2018 at 19:26
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Bhishan for the info 👍
@devPython you can thank by clicking the up arrow button on the answer. Good luck.
1

Break down what's needed: you need to convert to a string, to figure out how many characters to replace, generate a replacement string of that length, then include the tail of the original string. Also you need to be robust against, eg, strings too short to have any characters replaced.

'X' * max(0, len(str(number)) - 4) + str(number)[-4:]

For the second part: use a library.

Doing this directly is more complicated than it might seem to a beginner, because you're having to communicate with the systems which take text entry. It's going to depend upon the operating system, Windows vs "roughly everything else". For text entry outside of a web-browser or a GUI, most systems are emulating ancient text-only terminal devices because there's not yet enough reason to change that. Those devices have modes of text input (character at a time, line at a time, raw, etc) and changing them to not immediately "echo" the character typed involves some intricate system calls, and then other programming to echo a different character instead.

Thus you're going to want to use a library to take care of all those intricate details for you. Something around password entry. Given the security implications, using tested and hardened code instead of rolling your own is something I strongly encourage. Be aware that there are all sorts of issues around password handling too (constant time comparisons, memory handling, etc) such that as much as possible, you should avoid doing it at all, or move it to another program, and when you do handle it, use the existing libraries.

If you can, stick to the Python standard library and use getpass which won't echo anything for passwords, instead of printing stars.

If you really want the stars, then search https://pypi.org/ for getpass and see all the variants people have produced. Most of the ones I saw in a quick look didn't inspire confidence; pysectools seemed better than the others, but I've not used it.

answered Aug 25, 2018 at 19:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.