简体   繁体   中英

How do I replace name1 and name2 with an input string in Python using escaping?

I'm new to Python and this challenge is difficult for me to figure out. I have to: Pass in 2 strings, name1 and name2. You should output another string in the format

name1's done and name2's done with a new line between the two and where name1 and name2 are replaced with the input string.

# Input from the command line
import sys
name1 = sys.argv[1]
name2 = sys.argv[2]

# Your code goes here
newString = 'sys.argv[1]\'s done\nand name2\'s done'
print(newString)

My error message: Program Failed for Input: 1 2 Expected Output: 1's done and 2's done Your Program Output: sys.argv[1]'s done and name2's done

Your output was incorrect.

Use format to print this output:.

newString = '{}\'s done and {}\'s done'.format(name1, name2)
print(newString)

您可以如下所示简单使用%s

newString = '%s\'s done and %s\'s done' % (name1, name2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM