简体   繁体   中英

How can you add multiple values into a single key in a dictionary without it having commas?

In a question I asked a while ago, I got to try PyInquirer to edit multiple lines of a .txt file at once. I tried fiddling with it, when I found a problem.

This is the code:


question = [
    {
        'type': 'input',
        'name': 'text_lines',
        'message': '',
        'default': ('This is a test line\n' 'This is another test line\n')
    }
]

answer = prompt(question)
print('The file says:\n {}'.format(answer['text_line']))

As you can see, the key 'default' used parentheses but there are no commas between the strings. When I put commas between them, the error TypeError: not all arguments converted during string formatting occurs. How can I put two strings with the same key??? It was confusing to me because even the usage of square and curly brackets don't work.

I just want to recreate the 'default' key above without commas

Edit: I didn't realize it was just concatenation. Thanks for your answers!!

Since there are no commas, that is not a tuple: it's an expression evaluation, and the expression is the concatenation of two strings. Try it in interactive mode:

>>> ('This is a test line\n' 'This is another test line\n')
'This is a test line\nThis is another test line\n'

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