I have a dict_name which is having values coming from the user.
dict_name = { 'filename' : 'abc.pdf' }
list_name = [f'<_io.BufferedReader name='D:\\test\\{filename}'>']
But this is giving me invalid syntax error.
Required:
list_name = [<_io.BufferedReader name='D:\\test\\abc.pdf'>]
You can't use nested single quotes. One approach is to use triple quotes:
filename = 'test'
list_name = [f'''<_io.BufferedReader name='D:\\test\\{filename}'>''']
output: ["<_io.BufferedReader name='D:\\test\\test'>"]
Or, to alternate single and double quotes:
list_name = [f"<_io.BufferedReader name='D:\\test\\{filename}'>"]
Or, to escape the single quotes:
list_name = [f'<_io.BufferedReader name=\'D:\\test\\{filename}\'>']
Given your update, you should do:
dict_name = { 'filename' : 'abc.pdf' }
f"<_io.BufferedReader name='D:\\test\\{dict_name['filename']}'>"
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.