Search code examples
pythonwindowspasswordsconversion-specifier

How to write multiple different data types


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!


Solution

  • 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}')