Hello Python experts,
can someone please explain me why this code produces a syntax error:
On the other hand, the following code works fine:
Another solution is:
Interesting, when using double quotes ["sarah"] code snippet works as intended.
I'm using Python 3.8.5.
I know that the problem is caused by "closing the string" but escape (\) doesn't solve it.
I don't like mixing " and ' when working with strings, but this time I need to make an exception or to use an auxiliary variable.
can someone please explain me why this code produces a syntax error:
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
print(f'Sarah\'s favorite language is {favorite_languages['sarah']}.') On the other hand, the following code works fine:
print(f'Sarah\'s favorite language is {favorite_languages["sarah"]}.') Another solution is:
print(f"Sarah\'s favorite language is {favorite_languages['sarah']}.") Interesting, when using double quotes ["sarah"] code snippet works as intended.
I'm using Python 3.8.5.
I know that the problem is caused by "closing the string" but escape (\) doesn't solve it.
I don't like mixing " and ' when working with strings, but this time I need to make an exception or to use an auxiliary variable.
