0

I am doing a "create a password" project for my class in python. I just learned common conversion specifiers and they would like me to use this in my program.

So far I am stuck on the "second password", see code below:

# FIXME (1): Finish reading another word and an integer into variables. 
# Output all the values on a single line
favoriteColor = input('Enter favorite color: \n')
petName = input('Enter pet\'s name: \n')
passNumber = input('Enter a number: \n') 
print(favoriteColor, petName, passNumber)
# FIXME (2): Output two password options
password1 = favoriteColor
print('First password: %s_%s' % (favoriteColor,petName))
print('Second password: **%d','%s','%d'** % (passNumber, petName, passNumber))

I need to make the second password generated like: 6yellow6 My problem is I cannot figure out how to use % conversion next to each other without a space. Help please!

Emiswelt
  • 3,909
  • 1
  • 38
  • 56
Alita
  • 9
  • 1
  • 1
  • 3

2 Answers2

1

you can do string formating in many ways

  1. using %s
print('str - %s  number - %d' %('some_string', 100))

  1. using .format
print('str - {} number - {}'.format('some_string',100))

  1. using f-string (from python 3.6)
print(f'str - {some_string} number - {100}')

so for your answer

print('Second password: **%d%s%d**' % (passNumber, petName, passNumber))

or better

print(f'second password: {passNumber}{petName}{passNumber}')
Kartik Gautam
  • 257
  • 3
  • 12
  • print('Second password: **%d%s%d**' % (passNumber, petName, passNumber)) when I do the ** like you had, it shows up in the print, we haven't learned the f-string yet, with the {} i assume I need to make the password as a list? If so do you know how to make a list with user inputs? – Alita Jan 17 '20 at 21:42
  • @Saennia _with the {} i assume I need to make the password as a list?_ You should be able to do this without making a list. – AMC Jan 17 '20 at 21:48
  • @Saennia I added the ** because I thought you wanted them in your output. it will work without it and as for your second question ... you can use {} with simple sting too.. – Kartik Gautam Jan 17 '20 at 21:56
  • in fact you can put expressions in {} like {5+4} – Kartik Gautam Jan 17 '20 at 21:57
0

I will be using these strings so that I don't have to retype the input every single time.

favorite_color = 'Red'
pet_name = 'Gabriella'
pass_number = '11'

1. %-formatting

I'm not sure what the ** were for in your code.

pass_opt_1 = '%s%s' % (favorite_color, pet_name)
pass_opt_2 = '%s%s%s' % (pass_number, pet_name, pass_number)

Notice that I'm using %s for the pass_number. This is because input() always returns a string.


2. F-strings

F-strings are the newest string formatting method in Python.

pass_opt_1 = f"{favorite_color}{pet_name}"
pass_opt_2 = f"{pass_number}{pet_name}{pass_number}"

3. Concatenating strings

Since the passwords are simple to build, we can simply concatenate the various strings using +.

pass_opt_1 = favorite_color + pet_name
pass_opt_2 = pass_number + pet_name + pass_number

Notice the variable names. Let me know if you have any questions :)

AMC
  • 2,642
  • 7
  • 13
  • 35