-1

I want to understand the following code: first[_last_three_letter_firstName -3:], but how does this work? because len will count the number of characters, and how does that behave? Because in temp_passwd receives first[and the value obtained by len() - 3:]:

first_name = 'Matheus'
last_name = 'Silva'

   def passwd_generator(first, last):
      _last_three_letter_firstName = len(first)
      _last_three_letter_lastName = len(last)

      temp_passwd = first[_last_three_letter_firstName -3:] + last[_last_three_letter_lastName -3:]

      return temp_passwd

_test_passwd = passwd_generator(first_name, last_name)
print(_test_passwd)
xilpex
  • 3,097
  • 2
  • 14
  • 45
Clisman
  • 7
  • 2
  • 1
    in this Post you can find a Good answer: https://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python – Rene May 04 '20 at 01:14
  • And here you have a good tutorial like explanation in number 3. and 5.: https://snakify.org/en/lessons/strings_str/ – Rene May 04 '20 at 01:19
  • "How does len()?" is not a grammatically complete question, and the rest of your question doesn't help to clarify what exactly you want an answer to. – kaya3 Jul 24 '21 at 10:22

3 Answers3

1

Actually, you don't need to know the length of a string

str = "Hello world"
str[-3:]

and

str = "Hello world"
str[len(str)-3:]

both will return the last three characters

'rld'

By the way, you can shorten whole passwd_generator function to this:

def passwd_generator(first, last):
    return f'{first[-3:]}{last[-3:]}'

if you use Python3, of course

Anar Salimkhanov
  • 729
  • 10
  • 12
0

The len() function would return the length of the _last_three_letter_firstName and the index slice first[_last_three_letter_firstName -3:] would return the slice from three before the last character to the end.

So in this case first[_last_three_letter_firstName -3:] would return 'eus'.

You could follow similar steps for last[_last_three_letter_lastName -3:].

aj3409
  • 186
  • 2
  • 14
0

Please don't use colon there in the temp_password because the len() function calculate the length of the variable and slicing begin from 0 and you are trying to subtract with 3 how could you the you desire output and when use slicing

temp_passwd = first[_last_three_letter_firstName -3:] + last[_last_three_letter_lastName -3:] 

in type of statement don't use colon.

And when you have a bunch of rows and columns and you wanted to get the particular row or column so that time you can use slicing over there understood

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
manish hedau
  • 77
  • 1
  • 7